繁体   English   中英

如何检查父元素是否包含某个标签? -聚合物

[英]How to check if parent element contains a certain tag? - Polymer

我将代码分成不同的文件(简化版):

admin-page.html

<paper-tabs selected={{selected}} attr-for-selecton="name">
    <paper-tab name="User">User</paper-tab>
    ...
    ...
</paper-tabs>
<iron-pages selected={{selected}} attr-for-selection="name">
    <admin-user-page name="User"></admin-user-page>
    ...
    ...
</iron-pages>

admin-user-page.html

<!-- general page content... -->

// Javascript
//--------------
// here I want to have an if statement for whether the parent (user-page.html) 
// contains the <paper-tabs> tag.

记住我正在寻求PolymerJS解决方案,是否有任何方法可以从admin-user-page.html文件中检测admin-page.html中是否存在paper-tabs

好吧,在您的子组件(管理员用户页面)中,您可以使用this.parentNode访问您的父组件(管理员页面),然后检查是否有paper-tabs标签。 所以看起来像这样:

Polymer('admin-user-page', {
  ready: function(){
     // you have to use parentNode twice
     // this.parentNode is the iron-pages
     // this.parentNode.parentNode is the admin-page
     if( this.parentNode.parentNode.querySelector('paper-tabs') ){
       // paper-tabs tag exists in parent
     }
  }
});

但是,这不是一个优雅的解决方案。 但我希望能回答您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM