使用querySelector查找Polymer模板内的嵌套元素将返回null

时间:2014-07-08 08:27:57

标签: javascript polymer shadow-dom

我正在尝试在新元素(tabs-list)中使用paper-tabs,但在打印标签后,我无法使用querySelector来更改所选的。

元素代码(没有样式):

<link rel="import" href="../components/polymer/polymer.html">
<link rel="import" href="../sprint-service/sprint-service.html">
<link rel="import" href="../components/paper-tabs/paper-tabs.html">

<polymer-element name="tab-list" attributes="show">
  <template>
    <sprint-service id="service" sprints="{{sprints}}"></sprint-service>   
    <paper-tabs selected="all" valueattr="name" self-end>
      <paper-tab name="all">ALL</paper-tab>
      <template repeat="{{sprint in sprints}}">
        <paper-tab name="{{sprint.id}}">{{sprint.id}}</paper-tab>
      </template>
    </paper-tabs>
  </template>
  <script>
    Polymer('tab-list', {
      ready: function() {
        var tabs = document.querySelector('paper-tabs');
        tabs.addEventListener('core-select', function() {
          list.show = tabs.selected;
        }) 
      } 
    });
  </script>
</polymer-element>

Index.html代码(whitout样式):

<!doctype html>
<html>

<head>
  <title>unquote</title>
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
  <script src="../components/platform-dev/platform.js"></script>
  <link rel="import" href="../components/font-roboto/roboto.html">
  <link rel="import"
    href="../components/core-header-panel/core-header-panel.html">
  <link rel="import"
    href="../components/core-toolbar/core-toolbar.html">
  <link rel="import" href="tab-list.html">
  <link rel="import" href="post-list.html">
</head>

<body unresolved touch-action="auto">
  <core-header-panel>
    <core-toolbar>
      <tab-list></tab-list>
    </core-toolbar>
    <div class="container" layout vertical center>
      <post-list show="all"></post-list>
    </div>
  </core-header-panel>

  <script>

  var list = document.querySelector('post-list');

  </script>
</body>

</html>

但是

querySelector('paper-tabs') = *null*

我已尝试将eventListener放入 index.html ,但我遇到了同样的问题。 谁能告诉我问题出在哪里?

非常感谢!

2 个答案:

答案 0 :(得分:39)

document.querySelector('paper-tabs');

找不到 paper-tabs 元素,因为它隐藏在 tab-list 元素的shadow DOM中。

你可以简单地给 paper-tabs 一个id,比如说标签,然后像这样访问它

this.$.tabs

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

还可以选择直接访问shadow DOM

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

如果您只想在 paper-tabs 选项上收听更改,可以使用更改观察器:

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

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

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

答案 1 :(得分:0)

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

在我使用的dom-repeat中切换铁塌陷元素

 _showdetail: function(e){
    Polymer.dom(e.currentTarget).querySelector('#collapse').toggle();
},