删除html中的元素

时间:2019-05-05 01:33:29

标签: javascript html

当我单击删除按钮并过快地删除项目时...所有项目都会立即被删除。不是一个接一个...(不要对我加重...我只有4个小时的javascript)

https://imgur.com/a/ieb0BtD

enter image description here

count = 0;
cnt = 0;
array = [];

function add() {
var x = document.getElementById("add").value;

    if(x != ""){
        var uppercaseFirstLetter = x.charAt(0).toUpperCase();
        var stringWithoutFirstLetter = x.slice(1)
        var div = document.createElement('div');
        div.setAttribute("id", x);
        array[count] = x;
        div.className = 'a';

        div.innerHTML =
        '<label id="asd">' + uppercaseFirstLetter + stringWithoutFirstLetter + '</label>\
        <img id="delete" src="https://image.flaticon.com/icons/svg/148/148973.svg" onClick="reply_click(parentNode.id)">\
        <img id="tick" src="https://image.flaticon.com/icons/svg/149/149148.svg" onClick="reply_click(this.id)">';

    document.getElementById('w').appendChild(div);
    document.getElementById('add').value = '';
    document.getElementById("add").focus();
    count++;

    }
}

function reply_click(clicked_id)
{   

    if(clicked_id != "tick"){


var elem = document.getElementById(clicked_id);
elem.remove();

        document.getElementById("add").focus();
    }
    else{
        alert("tick");

    }
}

1 个答案:

答案 0 :(得分:0)

   function reply_click(){
        var item = this.parentNode.parentNode;
        var parent = item.parentNode;
        var id = parent.id;
        var value = item.innerText;
        parent.removeChild(item);
    }

此代码可能对您有用。 您可以观看现场演示。 http://codeleson.com/todolist/