亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 JavaScript 中的一個函數從多個函數中刪除一個特定的 div?

如何使用 JavaScript 中的一個函數從多個函數中刪除一個特定的 div?

慕仙森 2022-09-29 15:34:59
我正在學習 JavaScript,這對我來說是一個練習場景。我已經有一個克隆內容的按鈕,在克隆的內容中,有一個按鈕可以刪除它。當我單擊提示您刪除內容的按鈕時,它會刪除第一組內容。我希望發生的事情是,當您單擊提示您刪除內容的按鈕時,它會刪除與該按鈕相關的內容,而不會刪除其他任何內容。這是代碼圍欄鏈接。https://codepen.io/JosephChunta/pen/YzwwgvQ這是代碼。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 whichdocument.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>
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

當您嘗試按 ID 刪除時,它會獲取找到的第一個 ID。


要刪除正確的內容,請發送點擊。this


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

并在您的函數中處理它:


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>


查看完整回答
反對 回復 2022-09-29
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

在移除按鈕中,執行以下操作:

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

在你的腳本中:

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


查看完整回答
反對 回復 2022-09-29
  • 2 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號