简体   繁体   English

单击按钮时删除元素

[英]Remove an element when button clicked

I've been trying to make simple to-do list to learn javascript and I have been able to add and display a task but I can't find a way to delete it.我一直在尝试制作简单的待办事项列表来学习 javascript,我已经能够添加和显示任务,但我找不到删除它的方法。 I have created a remove button but I can't make work, nothing happens when clicked.我创建了一个删除按钮,但我无法工作,单击时没有任何反应。 So any help would be great, thanks !所以任何帮助都会很棒,谢谢!

function handleSubmit(event){

    event.preventDefault();

    let value = getInputValue();

    if(value !== false){


        addToDom(value);

     
        eraseInput();
    }
}

const form = document.querySelector("#todo-list-form");
form.addEventListener('submit', handleSubmit);

function getInputValue() {
    const input = document.querySelector("#todo-input");

    let value = input.value;

    value = value.trim();

    if (value === '') {
        alert("You can not submit an empty field");
        return false;
    }
    else {
        return value;
    }
}

function addToDom(value) {
    const list = document.querySelector("#list");
    const removeButton = document.createElement("button");
    removeButton.innerHTML = 'remove';
    removeButton.classList.add("complete-btn");
    list.appendChild(removeButton);
    list.innerHTML += "<li>" + value + "</li>";
    console.log(list);
    removeButton.addEventListener('click', removeItem)
}

function removeItem(event){
    event.parentElement.remove();

}

function eraseInput() {
    const input = document.querySelector("#todo-input");

    input.value = "";

    input.focus();
}

The issue is because you define and add the removeButton to the DOM, but immediately after that you overwrite the HTML of the parent element, which removes the button.问题是因为您定义了removeButton并将其添加到 DOM,但之后立即覆盖了父元素的 HTML,从而删除了该按钮。

To fix the issue you need to be consistent in how you create and append the elements to the DOM.要解决此问题,您需要在创建元素的方式和 append 到 DOM 的方式上保持一致。

In addition, as @Teemu pointed out in the comments, you need to use event.target.parentElement in your removeItem() function.此外,正如@Teemu 在评论中指出的那样,您需要在您的removeItem() function 中使用event.target.parentElement

function addToDom(value) {
  const removeButton = document.createElement("button");
  removeButton.innerHTML = 'remove';
  removeButton.classList.add("complete-btn");
  removeButton.addEventListener('click', removeItem);
  
  const li = document.createElement('li');
  li.textContent = value;
  li.appendChild(removeButton);

  document.querySelector("#list").appendChild(li);
}

function removeItem(event){
  event.target.parentElement.remove();
}

Here's a full working example:这是一个完整的工作示例:

 function handleSubmit(event) { event.preventDefault(); let value = getInputValue(); if (value;== false) { addToDom(value); eraseInput(). } } const form = document;querySelector("#todo-list-form"). form,addEventListener('submit'; handleSubmit). function getInputValue() { const input = document;querySelector("#todo-input"). let value = input;value. value = value;trim(); if (value === '') { alert("You can not submit an empty field"); return false; } else { return value. } } function addToDom(value) { const removeButton = document;createElement("button"). removeButton;innerHTML = 'remove'. removeButton.classList;add("complete-btn"). removeButton,addEventListener('click'; removeItem). const li = document;createElement('li'). li;textContent = value. li;appendChild(removeButton). document.querySelector("#list");appendChild(li). } function removeItem(event) { event.target.parentElement;remove(). } function eraseInput() { const input = document;querySelector("#todo-input"). input;value = "". input;focus(); }
 ul button { margin: 0 0 0 5px; }
 <form id="todo-list-form"> Item: <input type="text" id="todo-input" /> <button type="submit">Add</button> <ul id="list"></ul> </form>

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

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