javascript 未捕获的错误 NOT_FOUND_ERR DOM 异常 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7592563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:37:42  来源:igfitidea点击:

uncaught error NOT_FOUND_ERR DOM Exception 8

javascriptexceptionappendchild

提问by

So I am deleting all the contents under a particular div and adding a message content. However, javascript throw the following error after the finish:

所以我删除了特定 div 下的所有内容并添加了消息内容。但是,javascript 在完成后抛出以下错误:

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

Here is the code where it is executed

这是它执行的代码

 new Ajax.Request("profileThis.php",
 {
   method:'post',

   parameters:{title:title, review:review, userId:userId, category:category, categoryId:categoryId},

   onSuccess:function(ajax) 
   {
    alert(ajax.responseText); // this is just for debugging purposes

    var message=ajax.responseText;

    var divMessage=document.createElement("div");

    divMessage.style.color="rgb:(105,105,105)";

    divMessage.innerHTML=message;

    while($("reviewSheet").hasChildNodes)
    {
     $("reviewSheet").removeChild($("reviewSheet").lastChild);
    }

    $("reviewSheet").adopt(divMessage);         

   },

   onFailure:ajaxFailure,

   onException:ajaxFailure

 });

People commented that the problem was with how I assigned divMessageto reviewSheet. I tried both adoptand appendChildbut none works. A little help would be appreciated.

人们评论说,问题在于我如何分配divMessagereviewSheet. 我两个都试过了adoptappendChild但都没有效果。一点帮助将不胜感激。

采纳答案by epascarello

divMessage.style.color="rgb:(105,105,105)";

should be

应该

divMessage.style.color="rgb(105,105,105)";

回答by wuliwong

Is the problem that you are calling the method hasChildNodes() on a jQuery object? I'm not sure what $("reviewSheet") is supposed to be, but wrapping a string in $() makes it a jQuery object which I don't believe will work with regular javascript methods. If "reviewSheet" is the id of an element you could do something like

问题是您在 jQuery 对象上调用方法 hasChildNodes() 吗?我不确定 $("reviewSheet") 应该是什么,但是在 $() 中包装一个字符串使它成为一个 jQuery 对象,我认为它不能与常规的 javascript 方法一起使用。如果“reviewSheet”是元素的 id,您可以执行以下操作

node = document.getElementById('reviewSheet');

then you could go into your while loop.

然后你可以进入你的while循环。

while (node.hasChildNodes()) {
 //the rest of your code here
}

Oh also you need to put the parenthesis after hasChildNodes() to return a boolean value.

哦,您还需要将括号放在 hasChildNodes() 之后以返回布尔值。