【问题标题】:Node not found error - despite node can be found in console找不到节点错误 - 尽管可以在控制台中找到节点
【发布时间】:2012-10-17 09:47:40
【问题描述】:

我正在尝试在文档的最顶部插入一段 HTML,看起来像这样:

<html>
<frameset onunload="unload()" onload="loadme()" id="topframeset" frameborder="0" framespacing="0" rows="45,19,10,0,*,22">
(...)
</frameset>                                                                                
</html>

好的 - 看起来很容易。在 Firebug 中我可以做到:

>>> document.getElementById("topframeset")

<frameset id="topframeset" onunload="unload()" onload="loadme()" frameborder="0" framespacing="0" rows="45,19,10,0,*,22">

所以它觉得很好。

然后:

document.insertBefore(document.createTextNode("<h1>hello</h1>"), document.getElementById("topframeset"))

Error: Node was not found
[Break On This Error]   

...ertBefore(document.createTextNode("<h1>hello</h1>"), document.getElementById("to...

发生了什么事?

【问题讨论】:

  • 也许这是因为您试图构建无效的 HTML 结构。框架集不能有 h1 同级。 w3.org/TR/html4/present/frames.html#h-16.2.1
  • 我想可能是这样。那么,除了 在文档中真的不可能有任何东西吗?

标签: javascript html frame


【解决方案1】:

您需要将&lt;h1&gt;-textNode 插入到某些东西中,例如正文

document.body.insertBefore(document.createTextNode("<h1>hello</h1>"),
document.getElementById("topframeset"))

它可以工作

【讨论】:

  • 很抱歉,我在尝试您的代码时遇到了同样的错误。未找到节点。也许我试图操纵的 HTML 结构被破坏了?
【解决方案2】:

davidkonrad 是对的,你需要在某处插入文本节点。

var frameSet = document.getElementById('topframeset');
var textNode = document.createTextNode('<h1>hello</h1>');
frameSet.parentNode.insertBefore(textNode, frameSet);

【讨论】:

    猜你喜欢
    • 2015-04-18
    • 2016-12-03
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多