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

前端 未结 2 785
萌比男神i
萌比男神i 2020-12-08 15:36

I\'m trying to use paper-tabs inside new element (tabs-list) but after print tabs I can\'t use querySelector to change selected one.

Element code (withou

相关标签:
2条回答
  • 2020-12-08 16:13
    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)

    0 讨论(0)
  • 2020-12-08 16:32
          <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();
    },
    
    0 讨论(0)
提交回复
热议问题