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

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

Jquery 不會(huì)對(duì) Jquery 創(chuàng)建的文本做出反應(yīng)

Jquery 不會(huì)對(duì) Jquery 創(chuàng)建的文本做出反應(yīng)

慕哥6287543 2023-06-09 15:55:20
我想要做的是在按下按鈕時(shí)將元素添加到列表中,然后再次按下按鈕能夠刪除任何項(xiàng)目。這是我的意思的最小工作示例:$('.delete-item').click(function() {  $(this).parent().text("DELETED")})$('button').click(function() {  $('ul').append(`<li>Appended item <a class="delete-item" href="#">Delete item</a></li>`)})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul>  <li>List item 1 <a class="delete-item" href="#">Delete item</a></li>  <li>List item 2 <a class="delete-item" href="#">Delete item</a></li></ul><button type="button">Add item</button>在這里,您可以將項(xiàng)目添加到無(wú)序列表并“刪除”已經(jīng)存在的項(xiàng)目,現(xiàn)在,您可以很好地刪除預(yù)制項(xiàng)目,但是如果您嘗試刪除附加項(xiàng)目,無(wú)論出于何種原因,您都可以't。這是如何固定的?謝謝!
查看完整描述

1 回答

?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

$('.delete-item').click(...)僅將事件偵聽(tīng)器附加到現(xiàn)有項(xiàng)目。像這樣使用事件委托:

$("ul").on("click",?".delete-item",?function()?{
????$(this).parent().text("DELETED")
});

使用事件委托,您可以將事件偵聽(tīng)器附加到 DOM 中已存在的祖先(此處為元素<ul>),并監(jiān)視后代上的事件,無(wú)論它們是否已經(jīng)存在或動(dòng)態(tài)添加。

演示:

$("ul").on("click", ".delete-item", function() {

? $(this).parent().text("DELETED")

});


$("button").click(function() {

? $("ul").append(`<li>Appended item <a class="delete-item" href="#">Delete item</a></li>`)

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul>

? <li>List item 1 <a class="delete-item" href="#">Delete item</a></li>

? <li>List item 2 <a class="delete-item" href="#">Delete item</a></li>

</ul>


<button type="button">Add item</button>


查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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