jQuery事件之on()方法綁定多個選取器,多個事件

來源:互聯網
上載者:User

標籤:http   ar   io   sp   on   bs   cti   ad   html   

這樣一個需求,如果用live()方法實現的話 非常簡單,容易理解。

  1. $(‘nav li, #sb-nav li, #help li‘).live(‘click‘, function () {
  2.    // code...
  3. });

jquery在1.7版本後,建議大家用on方法代替之前的bind、live、delegate方法。
那上面一句如果用on的話,怎麼寫呢?

其實查看live源碼就知道,live實際是委託doucment進行事件委派的。
按照這個思路,可以將on方法綁定到document即可。

  1. $(document).on(‘click‘, ‘#header .fixed-feedback-bn, #sb-sec .feedback-bn‘, function () {
  2. // code...
  3. });

還有一種情況,on()方法綁定多個事件,可以這樣寫:

  1. $("table.planning_grid").on({
  2. mouseenter: function() {
  3. // Handle mouseenter...
  4. },
  5. mouseleave: function() {
  6. // Handle mouseleave...
  7. },
  8. click: function() {
  9. // Handle click...
  10. }
  11. }, "td");

最後,用on()方法綁定多個選取器,多個事件則可以這樣寫:

    1. $(document).on({
    2. mouseenter: function() {
    3. // Handle mouseenter...
    4. },
    5. mouseleave: function() {
    6. // Handle mouseleave...
    7. },
    8. click: function() {
    9. // Handle click...
    10. }
    11. }, ‘#header .fixed-feedback-bn, #sb-sec .feedback-bn‘);

http://www.qingdou.me/1536.html

jQuery事件之on()方法綁定多個選取器,多個事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.