我試圖學習聚合物2.0,但我堅持從一個不同的陰影Dom元素的問題。這工作在聚合物1,但不再在聚合物2.0。寫這個的正確方法是什麼?它只是告訴我,targetText = Null
。聚合物2.0 getElementById在不同的ShadowDom
感謝您的幫助!
這是一個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>
是反正這兩個要素的相互關係?這意味着他們有一個孩子的父母關係? – Niklas