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

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

動態(tài)刪除列表元素

動態(tài)刪除列表元素

慕碼人8056858 2021-03-30 17:14:30
我計劃為待辦事項列表編寫JavaScript代碼,以將元素動態(tài)添加到無序列表以及每個元素的刪除按鈕。當(dāng)用戶單擊“刪除”按鈕時,我想刪除相應(yīng)的列表元素。如何動態(tài)刪除所述列表元素var button = document.querySelector("button");var input = document.querySelector("input");var ul = document.querySelector("ul");button.addEventListener("click",function(){    if((input.value.length)>0&&(input.value.includes("by")===true))    {        var li= document.createElement("li");        li.appendChild(document.createTextNode(input.value + " "));        ul.appendChild(li);        input.value="";        var nb= document.createElement("button");        nb.appendChild(document.createTextNode("REMOVE"));        li.appendChild(nb);    }    else if(input.value.includes("by")===false)    {        alert("Make Sure You Adhere to the Defined Format");    }});
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊

您可以添加一個onclick處理程序,然后單擊按鈕獲取parentNode并使用remove將其刪除


var button = document.querySelector("button");

var input = document.querySelector("input");

var ul = document.querySelector("ul");

button.addEventListener("click", function() {

  if ((input.value.length) > 0 && (input.value.includes("by") === true)) {

    var li = document.createElement("li");

    li.appendChild(document.createTextNode(input.value + " "));

    ul.appendChild(li);

    input.value = "";

    var nb = document.createElement("button");

    //nb.classList.add('remove');

    nb.onclick = removeThis; // added onclick handler

    nb.appendChild(document.createTextNode("REMOVE"));

    li.appendChild(nb);

  } else if (input.value.includes("by") === false) {

    alert("Make Sure You Adhere to the Defined Format");

  }

});



function removeThis() {

  this.parentNode.remove();

}

<ul></ul>

<input type='text'><button type='button'>Add</button>


查看完整回答
反對 回復(fù) 2021-04-08
  • 1 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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