为什么这是可行的?如果不指定哪一项,它如何知道要删除哪一项?JavaScript魔法?
let addButtton = document.getElementById('addButton');
let body = document.querySelector("body");
addButton.onclick = function addToList() {
let taskName = document.getElementById("AddToList").value;
let toDoList = document.createElement("input");
toDoList.setAttribute("type", "checkbox");
toDoList.id = "task";
body.appendChild(toDoList);
let deleteButton = document.createElement("button");
deleteButton.innerHTML = "Delete";
let label = document.createElement("label");
label.setAttribute("for", ("task"));
label.innerHTML = taskName;
body.appendChild(label);
body.appendChild(deleteButton);
deleteButton.onclick = function deleteTask() {
body.removeChild(label);
body.removeChild(toDoList);
body.removeChild(buttonDel);
body.removeChild(breakLn);
}
}发布于 2020-02-25 21:05:14
当您在label变量中创建项let label = document.createElement("label");时,它会将引用保留在其中。当您将body.removeChild(label);作为参数传递时,将其从DOM中完全删除。
https://stackoverflow.com/questions/60395198
复制相似问题