【问题标题】:Error with appendChild: Node cannot be inserted at the specified point in the hierarchyappendChild 出错:无法在层次结构中的指定点插入节点
【发布时间】:2012-05-31 10:16:09
【问题描述】:

函数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.createAttribute()时不小心使用document.createElement()

标签: javascript appendchild


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 2010-11-10
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多