【问题标题】:NOT_FOUND_ERR: DOM Exception 8 - javascriptNOT_FOUND_ERR:DOM 异常 8 - javascript
【发布时间】:2013-01-28 07:51:05
【问题描述】:

我的代码:

function SubmitCommentAJAX(i)
{
    var thecomment = i.parentNode.getElementsByClassName("styled")[0].innerHTML; 
    var commentBox = document.body.getElementsByClassName("commentsScroll")[0];
    var request = "http://localhost:8080/ituned.com/index?Event=Comment&PostTitle=<%=p.getTitle()%>&PostOwner=<%=p.getUsername_of_Owner()%>&comment="+thecomment;

    xmlhttp.open("POST",request,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {            
            var response=xmlhttp.responseXML.getElementsByTagName("theComment")[0].text;
            **commentBox.insertBefore(response, commentBox.firstChild);**
        }
    };
}

HTML:

<div class="commentsScroll" align="left"> 
    <div></div>             
    </div> 
</div>

我收到错误:NOT_FOUND_ERR: DOM Exception 8 for line commentBox.insertBefore(response, commentBox.firstChild);

但是commentBox 的定义很明确,因为当我检查alert(commentBox) 时,它会向我显示对象。

错在哪里?

【问题讨论】:

标签: javascript exception dom insertion


【解决方案1】:

insertBefore 采用 dom 节点,因此您必须将文本转换为文本节点

var response=xmlhttp.responseXML.getElementsByTagName("theComment")[0].textContent;
commentBox.insertBefore(document.createTextNode(response), commentBox.firstChild);

【讨论】:

  • 你说的很对!!但是在您没有放置 createtextnode 之前,这就是它不起作用的原因。谢谢!
  • 不仅是字符串,包含 DOM 节点的单元素数组对于 insertBefore 都是无效参数。为什么我认为splice 并不总是返回一个数组? :-)
猜你喜欢
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2012-10-26
  • 1970-01-01
相关资源
最近更新 更多