jquery live 區別

來源:互聯網
上載者:User

標籤:

http://www.360doc.com/content/13/1222/22/14022539_339358149.shtml

開始的時候在jQuery.1.7.1中使用了.live()覺得很好用,特別是在綁定事件之後再加入的元素的事件綁定上很方便(第一次live之後以後添加的元素就不需要綁定啦)

後來jQuery更新到1.9.1,頁面中的.live報錯:"has no method live", 後來查了文檔才知道在新版本中做了修改。

jQuery.1.8.1:

$("#liveID").live("click",function(){alert("live click");});

jQuery.1.9.1:

$(document).on("click","#liveID",function(){alert("live click");});

 

jQuery網站上這麼說的:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the.on() method for more information.

改進後的使用建議:

1 $(selector).live(events, data, handler); // jQuery 1.3+

2 $(document).delegate(selector, events, data, handler); // jQuery 1.4.3+

3 $(document).on(events, selector, data, handler); // jQuery 1.7+

樣本:
1 $("a.offsite").live("click", function(){ alert("Goodbye!"); }); // jQuery 1.3+

2 $(document).delegate("a.offsite", "click", function(){ alert("Goodbye!"); }); // jQuery 1.4.3+

3 $(document).on("click", "a.offsite", function(){ alert("Goodbye!"); }); // jQuery 1.7+

jquery live 區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.