如何使用Polymer查询元素DOM的Selector元素

我有我的元素:
<dom-module id="x-el">
  <p class="special-paragraph">first paragraph</p>
  <content></content>
</dom-module>

我喜欢它

<x-el>
  <p class="special-paragraph">second paragraph</p>
</x-el>

在我的命令部分:

polymer({
  is: 'x-el',ready: function () {
    /* this will select all .special-paragraph in the light DOM
       e.g. 'second paragraph' */
    polymer.dom(this).querySelectorAll('.special-paragraph');

    /* this will select all .special-paragraph in the local DOM
       e.g. 'first paragraph' */
    polymer.dom(this.root).querySelectorAll('.special-paragraph');

    /* how can I select all .special-paragraph in both light DOM and
       local DOM ? */
  }
});

是否有可能使用polymer内置的?
或者我应该使用认的DOM api?

解决方法

polymer不提供辅助函数或抽象,它将列出来自light和local DOM的节点.

如果需要此功能,可以使用this.querySelector(selector).

另外,除了polymer.dom(this.root).querySelectorAll(selector)方法之外,polymer还提供了$$实用程序函数,该函数有助于访问元素本地DOM的成员.此功能使用如下:

<dom-module id="my-element">
  <template>
    <p class="special-paragraph">...</p>
    <content></content>
  </template>
</dom-module>

<script>
  polymer({
    is: 'my-element',ready: {
      this.$$('.special-paragraph'); // Will return the <p> in the local DOM
    }
  });
</script>

请注意,与querySelectorAll不同,$$函数只返回一个元素:本地DOM中与选择器匹配的第一个元素.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些