如何使用自定义聚合物元素访问元素?

时间:2014-08-07 02:23:12

标签: polymer web-component shadow-dom

给出示例元素。

<my-element>
    <h1>Hello world</h1>
    <a href="/test">Test</a>
</my-element>

如何选择标签的href

抱歉,我没有更多,我一直在测试很多东西。

如果您有<template>,我知道<content id="a-tag" select="a">。你应该能够this.$["a-tag"],但我不知道如何访问这些属性,再加上我在影子DOM中显示元素,这是我不想要的。思考?

2 个答案:

答案 0 :(得分:2)

content.getDistributedNodes()将为您提供通过<content>插入点的灯光节点列表。

如果您使用<content select="a">

,这应该可行
content.getDistributedNodes()[0].href

注意:this.$['a-tag']无效。您需要这样做:

this.$['a-tag'].getDistributedNodes()[0].href

答案 1 :(得分:1)

你试过this.querySelector('a').href吗? Polymer元素方法内部的this是指主机节点(在Shadow DOM之外)。

相关问题