1

我的代码:

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 the line commentBox.insertBefore(response, commentBox.firstChild);

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

错误是什么?

4

1 回答 1

1

insertBefore需要一个 dom 节点,因此您必须将文本转换为文本节点

var response=xmlhttp.responseXML.getElementsByTagName("theComment")[0].textContent;
commentBox.insertBefore(document.createTextNode(response), commentBox.firstChild);
于 2013-02-12T21:41:05.853 回答