Using querySelector to find nested elements inside a Polymer template returns null

泪湿孤枕 提交于 2019-11-28 06:24:51
document.querySelector('paper-tabs');

doesn't find the paper-tabs element, because it is hidden inside the shadow DOM of the tab-list element.

You can simply give paper-tabs an id, say tabs, and access it like so

this.$.tabs

(See http://www.polymer-project.org/docs/polymer/polymer.html#automatic-node-finding.)

There is also the option to access the shadow DOM directly

this.shadowRoot.querySelector('paper-tabs');

If you only want to listen for changes on the paper-tabs selection, you can use a change watcher:

<paper-tabs selected="{{currentTab}}">

Polymer('tab-list', {
  currentTab: 'all',
  currentTabChanged: function() {
    console.log(this.currentTab);
  }
});

(See http://www.polymer-project.org/docs/polymer/polymer.html#change-watchers)

      <template is="dom-repeat" items="{{dataobject}}">
        <div on-tap="_showdetail">
          <iron-collapse id="collapse">??</iron-collapse>
        </div>
      </template>

And to toggle the iron-collapse elements inside the dom-repeat I use

 _showdetail: function(e){
    Polymer.dom(e.currentTarget).querySelector('#collapse').toggle();
},
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!