javascript - HTML removeChild() 抛出未知异常

标签 javascript html exception

我的函数 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;" >&#10006</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/

相关文章:

exception - 加载文件 struts-default.xml 时捕获异常

c# - 如何捕获 ExtendedSocketException?

c++ - 未处理的异常在 0x00C87963 在 .... : 0xC0000005: Access violation reading location 0xCCCCCCE0

javascript - Angular 绑定(bind)字符串日期到输入类型日期

html - Tornado :为什么我会收到 http 500 错误

javascript - 使用 localStorage 中的值访问 JSON 数据

php - 使用 CSS 样式表格式化 PHP 值

html - 在 div 上拉伸(stretch)图像背景或在 div 中拉伸(stretch)完整背景图像

javascript - 单击时显示和隐藏选项卡内容

php - PHP/JavaScript/.NET 中的 MAC 地址?