【发布时间】:2014-12-15 22:43:06
【问题描述】:
我编写了下面的代码来获得一个带有文本和按钮的阴影 DOM,文本和按钮的样式都已正确设置,而主机背景的样式却以一种奇怪的方式设置,
代码是:
// Create a new object based of the HTMLElement prototype
var fonixDiv = Object.create(HTMLElement.prototype);
// Set up the element.
fonixDiv.createdCallback = function() {
// Create a Shadow Root
var shadow = this.createShadowRoot();
shadow.innerHTML = '\
<style>\
:host { \
border: 2px dashed red;\
text-align: left;\
font-size: 28px;\
background: blue;\
}\
h3 { color: red; }\
button { color: green; }\
</style>\
<h3>Shadow DOM</h3>\
<button id="d">click</button>\
';
shadow.children.d.addEventListener('click', function(e) {
this.textContent = "you clicked me :(";
shadow.children[1].textContent="Shadow DOM content changed";
host.style.background = "green";
alert("All: button, text and host should be change");
});
};
输出在附件中。
请提供任何帮助。
【问题讨论】:
-
你有什么问题?
-
为什么主机的背景不是蓝色的!以及为什么边界不在完整的历史范围内!如果我单击按钮,主机背景将根据需要变为绿色,
标签: javascript css custom-controls custom-component shadow-dom