Error with appendChild: Node cannot be inserted at the specified point in the hierarchy

别来无恙 提交于 2019-12-06 19:14:35

问题


There is an error with the function appendChild : Node 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/

Can you please help me?


回答1:


You need to append to document.body, not just document.

To explain why document.appendChild doesn't work consider the following diagram :

If that would be allowed that wouldn't be very useful since it will be a sibling of the HTML root element, that make it totally outside the content.

For more information : Using the W3C DOM Level 1 Core



来源:https://stackoverflow.com/questions/10744110/error-with-appendchild-node-cannot-be-inserted-at-the-specified-point-in-the-hi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!