2 回答
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
當(dāng)您嘗試按 ID 刪除時(shí),它會(huì)獲取找到的第一個(gè) ID。
要?jiǎng)h除正確的內(nèi)容,請(qǐng)發(fā)送點(diǎn)擊。this
<button onclick="removeContent(this)">Remove this</button>
并在您的函數(shù)中處理它:
function removeContent(el) {
el.parentNode.remove();
}
例:
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>
TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
在移除按鈕中,執(zhí)行以下操作:
<!-- The "this" keyword is a reference to the button element itself --> <button onclick="removeContent(this)">Remove this</button>
在你的腳本中:
function removeContent(element) {
element.parentNode.remove();
}添加回答
舉報(bào)
