简体   繁体   English

如何在 JavaScript 事件上创建删除按钮

[英]How to I create a remove button on a JavaScript event

Below is the js code I'm using to add elements to a TO DO List下面是我用来将元素添加到 TO DO 列表的 js 代码

let addButton = document.getElementById('sub-btn');
let listContainer = document.getElementById('items');
let inputField = document.getElementById('sub');
let rmvButton= document.getElementById('rmv');

//adds items to list after clicking button
addButton.addEventListener('click', function(){
    var paragraph = document.createElement('p');
    paragraph.classList.add('paragraph-style')
    paragraph.innerText = inputField.value;
    listContainer.appendChild(paragraph);
    inputField.value = ''
})

The exact implementation will depend on which element you want removed, but the general idea is to get a reference to the DOM element, and then call .remove() on it.确切的实现将取决于您要删除的元素,但总体思路是获取对 DOM 元素的引用,然后对其调用.remove() This example removes the last item in the list, if there is one:此示例删除列表中的最后一项(如果有的话):

 let addButton = document.getElementById('sub-btn'); let listContainer = document.getElementById('items'); let inputField = document.getElementById('sub'); let rmvButton= document.getElementById('rmv'); //adds items to list after clicking button addButton.addEventListener('click', function(){ var paragraph = document.createElement('p'); paragraph.classList.add('paragraph-style') paragraph.innerText = inputField.value; listContainer.appendChild(paragraph); inputField.value = '' }) rmvButton.addEventListener('click', function() { const lastItem = listContainer.children[listContainer.children.length-1]; if (lastItem) {lastItem.remove()} })
 <div id="items"> </div> <input id="sub" value="item text"> <button id="sub-btn">Add</button> <button id="rmv">Remove</button>

This second example demonstrates how to remove individual items, by attaching a specific remove button to each one as it's generated, with its own click handler:第二个示例演示如何删除单个项目,方法是在生成的每个项目上附加一个特定的删除按钮,并使用其自己的单击处理程序:

 let addButton = document.getElementById('sub-btn'); let listContainer = document.getElementById('items'); let inputField = document.getElementById('sub'); let rmvButton= document.getElementById('rmv'); //adds items to list after clicking button addButton.addEventListener('click', function(){ var paragraph = document.createElement('p'); paragraph.classList.add('paragraph-style') paragraph.innerText = inputField.value; // create and append a "remove" button to the item var rmv = document.createElement('button'); rmv.innerText="Remove"; paragraph.appendChild(rmv); // add a click handler to it which will remove this specific item, `paragraph`: rmv.addEventListener('click', function() { paragraph.remove(); }); listContainer.appendChild(paragraph); inputField.value = '' })
 <div id="items"> </div> <input id="sub" value="item text"> <button id="sub-btn">Add</button>

this removes an element这会删除一个元素

var myobj = document.getElementById("demo");
myobj.remove();

You can add a remove button by simply appending a button with a remove listener.您可以通过简单地附加一个带有删除侦听器的按钮来添加一个删除按钮。

const remove = document.createElement('button');
remove.textContent = 'Remove';
remove.addEventListener('click', handleRemove);

Here is a typical remove handler:这是一个典型的删除处理程序:

const handleRemove = e => {
  const item = e.target.closest('.item');
  item.parentElement.removeChild(item);
};

Full demo完整演示

 const addButton = document.getElementById('sub-btn'); const listContainer = document.getElementById('items'); const inputField = document.getElementById('sub'); const rmvButton = document.getElementById('rmv'); const handleRemove = e => { const item = e.target.closest('.item'); // Remove the listener, to avoid memory leaks. item.querySelector('.remove-btn').removeEventListener('click', handleRemove); item.parentElement.removeChild(item); }; // Adds items to list after clicking button addButton.addEventListener('click', e => { const item = document.createElement('div'); const paragraph = document.createElement('div'); const remove = document.createElement('button'); item.classList.add('item'); paragraph.classList.add('paragraph-style'); remove.classList.add('remove-btn'); paragraph.textContent = inputField.value; remove.textContent = 'Remove'; remove.addEventListener('click', handleRemove); item.append(paragraph); item.append(remove); listContainer.append(item); inputField.value = ''; })
 .items { display: flex; }.item { display: grid; grid-auto-flow: column; grid-template-columns: 1fr 6em; grid-column-gap: 1em; margin-bottom: 0.667em; align-items: center; }.paragraph-style { font-style: italic; }
 <div> <input type="text" id="sub" /> <button id="sub-btn">Add</button> </div> <hr /> <div id="items"></div>

You can create a button the same way you're creating the paragraph element.您可以像创建段落元素一样创建按钮。

// paragraph = ...
// ...
const button = document.createElement('button')
button.innerText = "Remove"
button.onclick = function() { paragraph.remove() }
// add button to the DOM

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

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