简体   繁体   English

如何删除特定的 ul 的 li?

[英]How can i remove an especific ul's li?

i'm trying to make some sort of a to do list.我正在尝试制作某种待办事项清单。 When i click the save button, it creates a new li with the input value, and it creates another ul's li with a remove button.当我单击保存按钮时,它会使用输入值创建一个新的 li,并使用删除按钮创建另一个 ul 的 li。 What i want now is to click the remove button and it remove that li.我现在想要的是单击删除按钮并删除该 li。 For some reason just the first button removes it, and it removes each li from first to last.出于某种原因,只有第一个按钮将其删除,并且从第一个到最后一个删除每个 li。

This the html:这是html:

<input type="text" id="itemname">
<button id="save">Salvar</button>
<main id="lists">
    <ul id="itemlist"></ul> 
    <ul id="listbutton"></ul>
</main>

And here's the js:这是js:

let list = document.getElementById("itemlist")
let buttonList = document.getElementById("listbutton")
let listItem = document.getElementById("itemname")
let button = document.getElementById("save")

button.onclick = saveItem

function removeitem(){
    let newli = document.querySelector("#itemlist li")
    list.removeChild(newli)
}

function saveItem(){
list.innerHTML += '<li>'+listItem.value+'</li>'
buttonList.innerHTML += '<li id="newitem"><button id="removebutton">Remover</button></li>'


let delBtn = document.querySelector("#listbutton button")

delBtn.onclick = removeitem

}

You might want to use the Event.target in your event listener:您可能希望在事件侦听器中使用Event.target

function removeitem(event){
    let newli = event.target.closest('li');
    list.removeChild(newli);
}

Edit: Additionally you're only attaching the event listener to one element, because querySelector only returns the first match.编辑:此外,您仅将事件侦听器附加到一个元素,因为querySelector仅返回第一个匹配项。 To attach the event listener to all elements, you'd need to either iterate:要将事件侦听器附加到所有元素,您需要迭代:

let delBtns = document.querySelectorAll("#listbutton button");
[...delBtns].forEach(el=> el.onclick = removeitem);

Or alternatively you'd need to attach a single event listener to a higher element, which would take advantage of event bubbling:或者,您需要将单个事件侦听器附加到更高的元素,这将利用事件冒泡:

let parentElement = document.querySelector("#listbutton")
parentElement.onclick = removeitem;

This is using Element.closest as the most robust/stable solution, but in your example since the button is a direct child of the li tag you could also take the simpler approach and use event.target.parentElement .这是使用Element.closest作为最强大/最稳定的解决方案,但在您的示例中,由于按钮是li标签的直接子级,您也可以采用更简单的方法并使用event.target.parentElement But if you changed the DOM structure in the future this could break.但是,如果您将来更改 DOM 结构,这可能会中断。

Try this, I've made some small tweaks to make it a little smoother.试试这个,我做了一些小调整,让它更平滑一点。

 // Create a "close" button and append it to each list item var myNodelist = document.getElementsByTagName("LI"); var i; for (i = 0; i < myNodelist.length; i++) { var span = document.createElement("SPAN"); var txt = document.createTextNode("\×"); span.className = "close"; span.appendChild(txt); myNodelist[i].appendChild(span); } // Click on a close button to hide the current list item var close = document.getElementsByClassName("close"); var i; for (i = 0; i < close.length; i++) { close[i].onclick = function () { var div = this.parentElement; div.style.display = "none"; } } // Add a "checked" symbol when clicking on a list item var list = document.querySelector('ul'); list.addEventListener('click', function (ev) { if (ev.target.tagName === 'LI') { ev.target.classList.toggle('checked'); } }, false); // Create a new list item when clicking on the "Add" button function newElement() { var li = document.createElement("li"); var inputValue = document.getElementById("myInput").value; var t = document.createTextNode(inputValue); li.appendChild(t); if (inputValue === '') { alert("You must write something!"); } else { document.getElementById("myUL").appendChild(li); } document.getElementById("myInput").value = ""; var span = document.createElement("SPAN"); var txt = document.createTextNode("\×"); // x for remove item span.className = "close"; span.appendChild(txt); li.appendChild(span); for (i = 0; i < close.length; i++) { close[i].onclick = function () { var div = this.parentElement; div.style.display = "none"; } } }
 <input type="text" id="myInput" placeholder="Title..."> <button onclick="newElement()" class="addBtn">Salvar</button> <main id="lists"> <ul id="myUL"></ul> </main>

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

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