【发布时间】:2017-08-03 18:54:51
【问题描述】:
我正在尝试学习 Polymer 2.0,但遇到从不同 Shadow Dom 获取元素的问题。这在 Polymer 1 中有效,但在 Polymer 2.0 中不再有效。写这个的正确方法是什么?它只是告诉我targetText = Null。
感谢您的帮助!
这是一个 MWE: 聚合物 WC 1:
<dom-module id="sc-navdrawer">
<template>
<style is="custom-style">
p {
font-weight: bold;
}
.changed {
color: red;
}
</style>
<p>Some text in normal p tag</p>
<div id="test" class="htmltextcontent" inner-h-t-m-l="{{inputText}}"></div>
</template>
<script>
Polymer({
is: 'sc-navdrawer',
properties: {
inputText: {
type: String,
value: "<p>Some innerhtml text in p tags</p>"
}
}
});
</script>
</dom-module>
聚合物 WC 2:
<dom-module id="sc-testpage">
<template>
<button onclick="{{changeColor}}">Click here to change color</button>
</template>
<script>
Polymer({
is: 'sc-testpage',
changeColor: function() {
var targetText = document.getElementById("test");
console.log(targetText);
targetText.classList.add("changed");
}
});
</script>
</dom-module>
【问题讨论】:
-
这两个元素是否相互关联?这意味着他们有一个孩子父母关系?