我的函数 removelink(id)
似乎一直存在错误,如下,在将项目添加到列表后调用,带有 onclick=""
行为。每次调用该函数时都会抛出异常:
"NotFoundError: Failed to execute removeChild' on 'Node': The node to be removed is not a child of this node."
我知道,通常这个错误会给出答案。但是,我仔细检查了 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;
}
在 HTML 中,我有一个 <div id ="listeditor">
加载,其中包含 div
带有 id 的 item1
, item2
, item3
等等...
里面那些div
更多 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+') ';
关于javascript - HTML removeChild() 抛出未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21582490/