[英]jQuery appending li to ul unclickable
我有以下代碼將數據添加到無序列表。 麻煩的代碼如下:
$('li:not(:first-child)').click(function() {
var clickedLi = this;
$(this).parent().prepend(clickedLi);
var selectedLi = $(this).text();
alert(selectedLi);
$(this).parent().parent().nextAll().children('ul').children().replaceWith('<li>Please select</li>');
//ajax call
var returnedData = "<li>Dataset1</li><li>Dataset2</li><li>Dataset3</li>";
//populate next with returned data
$(this).parent().parent().next().children('ul').append(returnedData);
});
如果單擊第一個ul中的某個選項,則會將數據添加到第二個ul。 但是,這個ul下的列表項是不可點擊的,我需要它們,所以可以繼續“鏈”。
這個bug在哪里? 任何幫助將非常感激。 提前致謝。
試試:(如果你使用大於1.6.8的jQuery。)
$(document).on('click','li:not(:first-child)',function() {
我可以假設,根據您的代碼判斷,您希望新添加的li繼承您附加到現有行為的行為。 不幸的是,它不能用這種方式編寫的代碼。
您需要以“可觀察”的方式附加click事件,以便新添加到dom的元素將附加事件處理程序。
為此,您必須使用.on
方法。 你可以在這里查看它的參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.