简体   繁体   English

如何从 HTML DOM 添加或删除特定元素

[英]How to add or remove specific element from the HTML DOM

I am having this confusion from a long time now, I want to add a specific element like the span in the example between the h1 and h2 tags, but when I click on the button, it added them as the last child of the container, I want to add at the middle of h1 and h2 tags, And on the remove button, I want to remove that specific element only instead of remove all elements, how can I remove that specific element when clicked on the button我长期以来一直有这种困惑,我想在示例中的 h1 和 h2 标签之间添加一个特定元素,例如span ,但是当我单击按钮时,它会将它们添加为容器的最后一个子元素,我想在 h1 和 h2 标签的中间添加,并在删除按钮上,我只想删除该特定元素而不是删除所有元素,单击按钮时如何删除该特定元素

 const container = document.querySelector('.container'); const btnAdd = document.querySelector('.btnAdd'); const btnRemove = document.querySelector('.btnRemove'); btnAdd.addEventListener('click', () => { container.innerHTML += `<span>Remove or Add me</span>`; }) btnRemove.addEventListener('click', () => { container.innerHTML += ``; })
 <div class="container"> <h1>Element</h1> <h2>Element</h2> </div> <button class="btnAdd">Add</button> <button class="btnRemove">Remove</button>

You need a few more lines of code to create a span element and work with it in the event handlers:您需要多几行代码来创建一个 span 元素并在事件处理程序中使用它:

 const container = document.querySelector('.container'); const btnAdd = document.querySelector('.btnAdd'); const btnRemove = document.querySelector('.btnRemove'); const h2 = document.querySelector('h2'); let addClicked = false; const newNode = document.createElement('span'); btnAdd.addEventListener('click', () => { if(addClicked) { return; } newNode.innerHTML = 'Remove or Add me'; container.insertBefore(newNode, h2); addClicked = true; }) btnRemove.addEventListener('click', () => { newNode.remove(); addClicked = false; })
 <div class="container"> <h1>Element</h1> <h2>Element</h2> </div> <button class="btnAdd">Add</button> <button class="btnRemove">Remove</button>

The other answer only lets you add/remove one single element.另一个答案只允许您添加/删除一个元素。

I've updated your snippet as you can run it below - this lets you keep adding elements (by first creating a span element with javascript and then using the insertBefore method on the h2 element to insert it dynamically).我已经更新了您的代码片段,您可以在下面运行它 - 这使您可以继续添加元素(首先使用 javascript 创建一个 span 元素,然后在 h2 元素上使用insertBefore方法动态插入它)。

The remove button then always deletes the last element that was added, allowing for a more dynamic list.然后,删除按钮总是删除添加的最后一个元素,从而允许更动态的列表。

You can take this one step further and add a delete button within each span, that upon being clicked, will delete any element wherever they are.您可以更进一步,在每个范围内添加一个删除按钮,单击该按钮后,将删除任何元素,无论它们在哪里。

 const container = document.querySelector('.container'); const btnAdd = document.querySelector('.btnAdd'); const btnRemove = document.querySelector('.btnRemove'); const lastElement = document.getElementById('last-element'); btnAdd.addEventListener('click', () => { const newSpan = document.createElement('span'); newSpan.innerHTML = 'Remove or add me'; container.insertBefore(newSpan, lastElement); }) btnRemove.addEventListener('click', () => { const lastSpan = Array.from(document.querySelectorAll('.container > span')).pop(); if (lastSpan) { container.removeChild(lastSpan); } })
 <div class="container"> <h1 id="first-element">Element</h1> <h2 id="last-element">Element</h2> </div> <button class="btnAdd">Add</button> <button class="btnRemove">Remove</button>

 const btnAdd = document.querySelector('.btnAdd'); const btnRemove = document.querySelector('.btnRemove'); const text = document.querySelector('.text'); btnAdd.addEventListener('click', () => { text.textContent = 'Remove or Add me' }) btnRemove.addEventListener('click', () => { text.textContent = ''; })
 <div class="container"> <h1>Element</h1> <span class="text"></span> <h2>Element</h2> </div> <button class="btnAdd">Add</button> <button class="btnRemove">Remove</button>

EDIT编辑

If you can't define element in html, you can try this code instead.如果您不能在 html 中定义元素,您可以尝试使用此代码代替。

 const container = document.querySelector(".container"); const h1 = document.querySelector(".container > h1"); const btnAdd = document.querySelector(".btnAdd"); const btnRemove = document.querySelector(".btnRemove"); btnAdd.addEventListener("click", () => { h1.insertAdjacentHTML("afterend", "<span>Remove or Add me</span>"); }); btnRemove.addEventListener("click", () => { const text = document.querySelector("h1 + span"); container.removeChild(text); });
 <div class="container"> <h1>Element</h1> <h2>Element</h2> </div> <button class="btnAdd">Add</button> <button class="btnRemove">Remove</button>

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

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