第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 JavaScript 中的一個(gè)函數(shù)從多個(gè)函數(shù)中刪除一個(gè)特定的 div?

如何使用 JavaScript 中的一個(gè)函數(shù)從多個(gè)函數(shù)中刪除一個(gè)特定的 div?

慕仙森 2022-09-29 15:34:59
我正在學(xué)習(xí) JavaScript,這對(duì)我來(lái)說(shuō)是一個(gè)練習(xí)場(chǎng)景。我已經(jīng)有一個(gè)克隆內(nèi)容的按鈕,在克隆的內(nèi)容中,有一個(gè)按鈕可以刪除它。當(dāng)我單擊提示您刪除內(nèi)容的按鈕時(shí),它會(huì)刪除第一組內(nèi)容。我希望發(fā)生的事情是,當(dāng)您單擊提示您刪除內(nèi)容的按鈕時(shí),它會(huì)刪除與該按鈕相關(guān)的內(nèi)容,而不會(huì)刪除其他任何內(nèi)容。這是代碼圍欄鏈接。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貢獻(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>


查看完整回答
反對(duì) 回復(fù) 2022-09-29
?
手掌心

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();
}


查看完整回答
反對(duì) 回復(fù) 2022-09-29
  • 2 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)