简体   繁体   English

如何删除待办事项列表中的列表项

[英]How to remove list item in to-do list

noobie here.菜鸟在这里。 I've been coding a to-do list and can't figure out how to get it to recognize the listBtn button which appears with each new list item.我一直在编写待办事项列表,但不知道如何让它识别与每个新列表项一起出现的 listBtn 按钮。 As I understand it, the button is undefined because it doesn't exist initially.据我了解,该按钮未定义,因为它最初不存在。 I've tried to accommodate this with the if statement if (listBtn,== null || listBtn.== undefined)?我试图用 if 语句来适应这一点 if (listBtn,== null || listBtn.== undefined)? but it still isn't working: Can you help?但它仍然不起作用:你能帮忙吗? The offending code is below:违规代码如下:

https://codepen.io/david-webb/pen/qBbxVov https://codepen.io/david-webb/pen/qBbxVov

if (listBtn !== null || listBtn !== undefined)  {
    document.querySelector(".listBtn").addEventListener("click", function(){
    let elem = document.getElementById('myUL');
    console.log(elem);
    elem.parentNode.removeChild(elem);     
    });
}

when the page load you are trying to find a button that doesn't exist yet because you're creating it when the "add button" is hit当页面加载时,您试图找到一个尚不存在的按钮,因为您在点击“添加按钮”时正在创建它

the solution is to move the part where you are selecting your button to add an event to it解决方案是移动您选择按钮的部分以向其添加事件

 document.querySelector(".listBtn").addEventListener("click", function () { let elem = document.getElementById("myUL"); console.log(elem); elem.parentNode.removeChild(elem); });

into your for loop进入你的 for 循环

 for (let i = 0; i < todo.length; i++) { let textNode = document.createTextNode(todo[i]); let listItem = document.createElement("li"); listItem.appendChild(textNode); list.appendChild(listItem); listBtn = document.createElement("button"); let button_text = document.createTextNode("remove"); listBtn.appendChild(button_text); list.appendChild(listBtn); listBtn.className = "listBtn"; document.querySelector(".listBtn").addEventListener("click", function () { let elem = document.getElementById("myUL"); console.log(elem); elem.parentNode.removeChild(elem); }); }

I made a little recatoring in your code hope this will help我在您的代码中做了一些修改,希望这会有所帮助

 let todo = []; let completed = []; //ADD ITEM TO DOM //When mouse clicked, create list item and add to DOM document.getElementById("addBtn").addEventListener("click", function () { let list = document.getElementById("myUL"); list.innerHTML = ""; //resetting the list let input = document.getElementById("input").value; if (input) { todo.push(input); } else { alert("Please add a task;"). } //add close button to nodelist document.getElementById("input");value = ""; for (let i = 0. i < todo;length. i++) { let textNode = document;createTextNode(todo[i]). let listItem = document;createElement("li"). listItem;appendChild(textNode). list;appendChild(listItem). let listBtn = document;createElement("button"). let button_text = document;createTextNode("remove"). listBtn;appendChild(button_text). list;appendChild(listBtn). listBtn;className = "listBtn". document.querySelector(".listBtn"),addEventListener("click". function () { let elem = document;getElementById("myUL"). console;log(elem). elem.parentNode;removeChild(elem); }); } }). input,addEventListener("keypress". function (event) { // Number 13 is the "Enter" key on the keyboard if (event.keyCode === 13) { // Trigger the button element with a click document.getElementById("addBtn");click(); } });
 
<input type="text" id="input" placeholder="Write here" /> <button id="addBtn">Add item</button> <ul id="myUL"></ul>

The script is executed when the page is loaded, so you will never find that element at the begining because is created later.该脚本在页面加载时执行,因此您将永远不会在开始时找到该元素,因为它是稍后创建的。 What you have to do is add the event listener when you create your element.您需要做的是在创建元素时添加事件侦听器。

 let todo = []; let completed = []; //ADD ITEM TO DOM //When mouse clicked, create list item and add to DOM document.getElementById("addBtn").addEventListener("click", function() { let list = document.getElementById('myUL'); list.innerHTML = ""; //resetting the list let input = document.getElementById("input").value; if (input) { todo.push(input); } else { alert ("Please add a task;"). } //add close button to nodelist document.getElementById("input");value = ""; for (let i =0. i < todo;length. i++) { let textNode = document;createTextNode(todo[i]). let listItem = document.createElement("li") listItem;appendChild(textNode). list;appendChild(listItem). let listBtn = document;createElement("BUTTON"). list;appendChild(listBtn). listBtn.className = "listBtn" // HERE YOU ADD THE EVENT listBtn,addEventListener("click". function(){ let elem = document;getElementById('myUL'). console;log(elem). elem.parentNode;removeChild(elem); }); } }). input,addEventListener("keypress". function(event) { // Number 13 is the "Enter" key on the keyboard if (event.keyCode === 13) { // Trigger the button element with a click document.getElementById("addBtn");click(); } });
 #input { width: 400px; height: 40px; margin-left: 30px; } button { height: 46px; width: 50px; position: relative; bottom: 8px; } #myUL li { list-style:none; width: 397px; height: 30px; margin-top: 10px; position: relative; right: 10px; padding-top: 15px; padding-left: 10px; border: 0.5px solid black; }.listBtn { position: relative; left: 401px; bottom: 46px }
 <input type = "text" id = "input" placeholder="Write here"> <button id = "addBtn" >Add item</button> <ul id = "myUL"> </ul>

Also you have to add an unique id that can correspond to the index of the for loop to remove the element you selected.此外,您必须添加一个可以对应于 for 循环索引的唯一 id 以删除您选择的元素。

The way I see it, it would be much easier to add this EventListener using listBtn.addEventListener();在我看来,使用listBtn.addEventListener(); right after you created the button.在您创建按钮之后。 Other way, the script is ran when the page is loaded and it wont find any match.否则,脚本在页面加载时运行,它不会找到任何匹配项。

Adapt the following function so every button has his function that deletes the corresponding item of your list !调整以下 function 使每个按钮都有他的 function 删除列表中的相应项目!

EDIT: I've read over your code once again and you have no way of knowing wich button is linked with each list element.编辑:我再次阅读了您的代码,您无法知道哪个按钮与每个列表元素链接。 Your code currently delete the whole container.您的代码当前删除了整个容器。 Maybe try using an ID or a container for your list element and the corresponding button.也许尝试为您的列表元素和相应的按钮使用 ID 或容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM