jjzjj

javascript - appendChild 错误 : Node cannot be inserted at the specified point in the hierarchy

coder 2024-05-12 原文

appendChild 函数出错:无法在层次结构中的指定点插入节点

JS:

var abc=document.createElement("div");
abc.style.position="absolute";
abc.style.width="10px";
abc.style.height="10px";
abc.style.left="10px";
abc.style.top="10px";
abc.style.backgroundColor="black";
abc.innerHTML="abc";
document.appendChild(abc);

http://jsfiddle.net/T7ZMX/

你能帮帮我吗?

最佳答案

您需要附加到 document.body,而不仅仅是 document

要解释为什么 document.appendChild 不起作用,请考虑下图:

如果允许这样做,那将不会很有用,因为它将成为 HTML 根元素的同级元素,这使得它完全在内容之外。

更多信息:Using the W3C DOM Level 1 Core

关于javascript - appendChild 错误 : Node cannot be inserted at the specified point in the hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744110/

有关javascript - appendChild 错误 : Node cannot be inserted at the specified point in the hierarchy的更多相关文章

随机推荐