微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用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中与选择器匹配的第一个元素.

原文地址:https://www.jb51.cc/html/227791.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐