简体   繁体   English

如何使用 JavaScript 中的一个 function 从多个 div 中删除特定的 div?

[英]How do I remove a specific div out of many using one function in JavaScript?

I'm learning JavaScript and this is a practice scenario for me.我正在学习 JavaScript,这对我来说是一个练习场景。

What I have already is a button that clones content, and within that content that has been cloned, there is a button to remove it.我已经有了一个克隆内容的按钮,在已克隆的内容中,有一个按钮可以将其删除。

When I click the button that prompts you to remove the content, it removes the first set of content.当我点击提示您删除内容的按钮时,它删除了第一组内容。

What I want to happen is when you click the button that prompts you to remove the content, it removes the content related to that button and nothing else.我想要发生的是,当您单击提示您删除内容的按钮时,它会删除与该按钮相关的内容,而不会删除其他任何内容。

This is the CodePen link.这是 CodePen 链接。

https://codepen.io/JosephChunta/pen/YzwwgvQ https://codepen.io/JosephChunta/pen/YzwwgvQ

Here is the code.这是代码。

 function addContent() { var itm = document.getElementById("newContent"); var cln = itm.cloneNode(true); document.getElementById("placeToStoreContent").appendChild(cln); } function removeContent() { var x = document.getElementById("content").parentNode.remove(); } // This is for debug purposes to see which content is which document.getElementById('orderContent').addEventListener('click', function(e) { const orderedNumber = document.querySelectorAll('.thisIsContent'); let i = 1; for (p of orderedNumber) { p.innerText = '' + (i++); } });
 .contentThatShouldBeHidden { display: none; }
 <div id="placeToStoreContent"> </div> <button id="orderContent" onclick="addContent()">Add Content</button> <div class="contentThatShouldBeHidden"> <div id="newContent"> <div id="content"> <p class="thisIsContent">This is a prompt</p> <button onclick="removeContent()">Remove this</button> <hr /> </div> </div> </div>

When you'r trying to remove by ID, it takes the first ID it finds.当您尝试按 ID 删除时,它会使用它找到的第一个 ID。

To remove the correct content, send this onclick.要删除正确的内容,请发送this onclick。

  <button onclick="removeContent(this)">Remove this</button>

And handle it in your function:并在你的 function 中处理:

function removeContent(el) {
  el.parentNode.remove();
}

Example:例子:

 function addContent() { var itm = document.getElementById("newContent"); var cln = itm.cloneNode(true); document.getElementById("placeToStoreContent").appendChild(cln); } function removeContent(el) { el.parentNode.remove(); } // This is for debug purposes to see which content is which document.getElementById('orderContent').addEventListener('click', function(e) { const orderedNumber = document.querySelectorAll('.thisIsContent'); let i = 1; for (p of orderedNumber) { p.innerText = '' + (i++); } });
 .contentThatShouldBeHidden { display: none; }
 <div id="placeToStoreContent"> </div> <button id="orderContent" onclick="addContent()">Add Content</button> <div class="contentThatShouldBeHidden"> <div id="newContent"> <div id="content"> <p class="thisIsContent">This is a prompt</p> <button onclick="removeContent(this)">Remove this</button> <hr /> </div> </div> </div>

In your remove button, do this:在您的删除按钮中,执行以下操作:

<!-- The "this" keyword is a reference to the button element itself -->
<button onclick="removeContent(this)">Remove this</button>

And in your javascript:在您的 javascript 中:

function removeContent(element) {
  element.parentNode.remove();
}

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

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