13

函数有错误appendChildNode cannot be inserted at the specified point in the hierarchy

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/

你能帮我么?

4

1 回答 1

33

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

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

DOM 树

如果允许这样做,那将不是很有用,因为它将是HTML根元素的兄弟,这使它完全在内容之外。

更多信息:使用 W3C DOM Level 1 Core

于 2012-05-24T19:33:24.120 回答