Javascript HTML removeChild()引发未知异常
我似乎对下面的函数Javascript HTML removeChild()引发未知异常,javascript,html,exception,Javascript,Html,Exception,我似乎对下面的函数removelink(id)有一个持续的错误,它是在一个项目添加到列表后调用的,具有onclick=”“行为。每次调用函数时,都会引发异常: NotFoundError:未能在“节点”上执行removeChild:要删除的节点 要删除的不是此节点的子节点。“ 我知道,通常错误会泄露答案。但是,我仔细检查了removeChild()在右父div中调用,以及右子div。然而,这直接导致了错误 我对JS非常缺乏经验。谢谢你的帮助 function removelink(id) {
removelink(id)
有一个持续的错误,它是在一个项目添加到列表后调用的,具有onclick=”“
行为。每次调用函数时,都会引发异常:
NotFoundError:未能在“节点”上执行removeChild:要删除的节点
要删除的不是此节点的子节点。“
我知道,通常错误会泄露答案。但是,我仔细检查了removeChild()
在右父div中调用,以及右子div。然而,这直接导致了错误
我对JS非常缺乏经验。谢谢你的帮助
function removelink(id) {
var itemname = 'item' + id;
if(confirm("Are you sure you want to remove this item from the list?" + itemname) == true) {
try {
CKEDITOR.remove('leditor'+ id);
document.getElementById('listeditor').removeChild(document.getElementById(itemname));
x--;
limit++;
document.getElementById("btnadditem") = '+ Add an item('+limit+') ';
renumber();
event.preventDefault();
}
catch(i) {
alert("Error Thrown: " + i);
return false;
}
}
return false;
}
/*
*(Purpose: Creates a new item based on the numbering of var x, an int starting at 0, and initiates the textbox + drawing)
*/
function addNewItem() {
if(limit >= 1){
var divcode = document.createElement();
divcode.innerHTML = '<div id="item'+ x +'"><div class="input-group"><span id="itemNumid'+ x +'" class="input-group-addon">'+ x +'</span><input type="email" class="form-control" name="header'+ x +'" placeholder="Item header"s tyle="border-bottom:none;" ><span class="input-group-addon" style="padding: 0px 0px;" ><button class="btn btn-danger btn-xs" id="btnremove'+x+'" onclick="removelink('+x+'); return false;" style="height: 41px; width: 41px;" >✖</button></span></div><textarea name="editor'+ x +'" id="leditor'+ x +'" class="form-control ckeditor" rows="8" style="resize:none;border-top:none;" placeholder="Item Content"></textarea><hr /></div>';
document.getElementById("listeditor").appendChild(divcode);
//CKEDITOR.inline( document.getElementById( 'editable' ) )
CKEDITOR.replace('leditor'+ x);
//createEditor(x);
//$( 'textarea#leditor' + x ).ckeditor();
//document.getElementById('leditor' + x).className += " ckeditor"
x++;
limit--;
if(limit != 0) {
document.getElementById("btnadditem").innerHTML = " + Add an item("+limit+") ";
}
else {
document.getElementById("btnadditem").innerHTML = "Limit Reached";
document.getElementById("btnadditem").className += "disabled";
}
}
return false;
}
函数移除链接(id){
var itemname='item'+id;
如果(确认(“是否确实要从列表中删除此项目?”+itemname)==true){
试一试{
CKEDITOR.remove('leditor'+id);
document.getElementById('listeditor').removeChild(document.getElementById(itemname));
x--;
极限++;
document.getElementById(“btnadditem”)='+添加一项('+limit+');
重新编号();
event.preventDefault();
}
捕获(i){
警报(“抛出错误:+i”);
返回false;
}
}
返回false;
}
/*
*(用途:基于var x的编号创建一个新项目,一个从0开始的整数,并启动文本框+图形)
*/
函数addNewItem(){
如果(限制>=1){
var divcode=document.createElement();
divcode.innerHTML='+x+'和#10006
;
document.getElementById(“listeditor”).appendChild(divcode);
//CKEDITOR.inline(document.getElementById('editable'))
CKEDITOR.replace('leditor'+x);
//createEditor(x);
//$('textarea#leditor'+x).ckeditor();
//document.getElementById('leditor'+x).className+=“ckeditor”
x++;
限制--;
如果(限制!=0){
document.getElementById(“btnadditem”).innerHTML=“+添加项目”(+limit+”);
}
否则{
document.getElementById(“btnadditem”).innerHTML=“达到限制”;
document.getElementById(“BtnadItem”).className+=“已禁用”;
}
}
返回false;
}
在HTML中,我有一个加载的
,其中包含ID为item1
,item2
,item3
,等等的div
在这些
div
s中,更多div
s用于组织代码的显示。将删除子级的行替换为:
var itemNode = document.getElementById(itemname);
itemNode.parentNode.removeChild(itemNode);
代码中的另一个问题是:
document.getElementById("btnadditem") = '+ Add an item('+limit+') ';
您不能用字符串覆盖html节点,您可能需要执行以下操作:
document.getElementById("btnadditem").innerHTML = '+ Add an item('+limit+') ';
删除了该异常,但现在我得到了另一个异常,它阻止调用函数renumber():ReferenceError:Invalid left-side inassignment@user3272700:再次检查答案并修复代码。