2013-03-07 87 views
1

我想使用jquery()的live()函數。但我不知道爲什麼,但它不工作。請幫幫我。jquery live不工作

$("#button_suchen").click(function(){ 
    $(".search_listing_content").append("< div class="listing dunkel"> 

< div class="listing_add" style="width:30px;height:30px;"></div>< div class="listing_name">Sepp Forcher</div></div>"); 
       }); 

    $(".listing_add").live("click",function(){ 
      alert("yes"); 
    }); 
+0

避免使用live(),而是使用on()或delegate()。 – 2013-03-07 15:33:16

+4

什麼版本的jQuery,我相信它在1.9(或可能,2+)中被刪除。 – 2013-03-07 15:33:18

+0

從官方網站: 從jQuery 1.7開始,.live()方法已被棄用。使用.on()附加事件處理程序。老版本的jQuery用戶應優先使用.delegate(),而不要使用.live()。 – 2013-03-07 15:35:19

回答

1

直播,因爲jQuery的1.7已被棄用,在1.9.0使用。對()中移除,而不是

$('.listing_add').on('click',function(){...}); 
+0

我已經嘗試過使用「on」,但它仍然不起作用:/ – 2013-03-07 15:35:28

+0

您使用的是什麼版本的jQuery? – Anton 2013-03-07 15:35:49

+0

jquery-1.9.1.min.js – 2013-03-07 15:36:08

1

嘗試。將您的js代碼移到文檔ready

$(function(){ 

    $(document).on("click",".listing_add",function(){ 
    alert("yes"); 
    }); 

}); 
+0

如果您綁定到文檔,它不需要在文檔準備就緒。 – 2013-03-07 15:41:29