Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:动态删除div_Javascript_Html_Dynamic - Fatal编程技术网

javascript:动态删除div

javascript:动态删除div,javascript,html,dynamic,Javascript,Html,Dynamic,我对javascript非常陌生。我正在创建的页面上有一堆表单,它们将保存不同的数据。我已经设法整合了下面的脚本,它允许我按ID添加更多的div 我现在想做的是能够为每个新的div添加一个按钮,允许删除特定的div var counter = 1; var limit = 30; function addInput(what) { if (counter == limit) { alert("You have reached the limit

我对javascript非常陌生。我正在创建的页面上有一堆表单,它们将保存不同的数据。我已经设法整合了下面的脚本,它允许我按ID添加更多的div

我现在想做的是能够为每个新的div添加一个按钮,允许删除特定的div

var counter = 1;
var limit = 30;
function addInput(what) {
        if (counter == limit)  {
                alert("You have reached the limit of adding " + counter + " inputs");
        }
        else {
        var container = document.getElementById(what);
        var clone = container.cloneNode(true);
                clone.setAttribute('id','div_'+document.getElementById(what).getElementsByTagName('div').length);
        container.appendChild (clone);
        counter++;
        }
}

把它放在container.appendChild(克隆)之前

谢谢。肯定会添加按钮。挑战在于,如果您将表单添加到两个不同的部分,则“删除”按钮将删除所有添加的内容,而不仅仅是最后一个。我不知道您的确切意思,但这应该只删除
克隆
元素。如果有一个表单可以添加更多cookie,另一个表单可以添加香蕉,您现在可以添加每个表单的更多内容。如果我在每个表单中添加一个表单,然后决定删除一个表单,那么这个脚本将删除我动态添加的所有新表单。
var remButton=document.createElement('button');
remButton.textContent='Remove';//or something else
remButton.addEventListener('click',function(){
    this.parentNode.parentNode.removeChild(this.parentNode)
},false);
clone.appendChild(remButton);