jQuery中on()方法用法執行個體詳解,jquery執行個體

來源:互聯網
上載者:User

jQuery中on()方法用法執行個體詳解,jquery執行個體

本文執行個體分析了jQuery on()方法的用法。分享給大家供大家參考。具體分析如下:

一、jQuery on()方法的使用:
 
on(events,[selector],[data],fn)
 
events:一個或多個用空格分隔的事件類型和可選的命名空間,如"click"或"keydown.myPlugin" 。
selector:一個選取器字串用於過濾器的觸發事件的選取器元素的後代。如果選取器為null或省略,當它到達選定的元素,事件總是觸發。
data:當一個事件被觸發時要傳遞event.data給事件處理函數。
fn:該事件被觸發時執行的函數。 false 值也可以做一個函數的簡寫,返回false。

二、jQuery on()方法的優點:
 
1、提供了一種統一綁定事件的方法

2、仍然提供了.delegate()的優點,當然如果需要你也可以直接用.bind()

三、與.bind(), .live(), .delegate()的比較:
 
1、其實.bind(), .live(), .delegate()都是通過.on()來實現的
複製代碼 代碼如下:bind: function( types, data, fn ) {
        return this.on( types, null, data, fn );
    },
    unbind: function( types, fn ) {
        return this.off( types, null, fn );
    },

    live: function( types, data, fn ) {
        jQuery( this.context ).on( types, this.selector, data, fn );
        return this;
    },
    die: function( types, fn ) {
        jQuery( this.context ).off( types, this.selector || "**", fn );
        return this;
    },

    delegate: function( selector, types, data, fn ) {
        return this.on( types, selector, data, fn );
    },
    undelegate: function( selector, types, fn ) {
        // ( namespace ) or ( selector, types [, fn] )
        return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn );
    }

2、用.bind()的代價是非常大的,它會把相同的一個事件處理常式hook到所有匹配的DOM元素上

3、不要再用.live()了,它已經不再被推薦了,而且還有許多問題

4、.delegate()會提供很好的方法來提高效率,同時我們可以添加一事件處理方法到動態添加的元素上。

5、我們可以用.on()來代替上述的3種方法

四、jQuery on()方法的使用樣本

1、綁定click事件,使用off()方法移除on()所綁定的方法
複製代碼 代碼如下:$(document).ready(function(){
$("p").on("click",function(){
$(this).css("background-color","pink");
});
$("button").click(function(){
$("p").off("click");
});
});

2、多個事件綁定同一個函數
複製代碼 代碼如下:$(document).ready(function(){
  $("p").on("mouseover mouseout",function(){
    $("p").toggleClass("intro");
  });
});

3、多個事件綁定不同函數
複製代碼 代碼如下:$(document).ready(function(){
  $("p").on({
    mouseover:function(){$("body").css("background-color","lightgray");}, 
    mouseout:function(){$("body").css("background-color","lightblue");},
    click:function(){$("body").css("background-color","yellow");} 
  });
});

4、綁定自訂事件
複製代碼 代碼如下:$(document).ready(function(){
  $("p").on("myOwnEvent", function(event, showName){
    $(this).text(showName + "! What a beautiful name!").show();
  });
  $("button").click(function(){
    $("p").trigger("myOwnEvent",["Anja"]);
  });
});

5、傳遞資料到函數
複製代碼 代碼如下:function handlerName(event)
{
  alert(event.data.msg);
}

$(document).ready(function(){
  $("p").on("click", {msg: "You just clicked me!"}, handlerName)
});

6、適用於未建立的元素
複製代碼 代碼如下:$(document).ready(function(){
  $("div").on("click","p",function(){
    $(this).slideToggle();
  });
  $("button").click(function(){
    $("<p>This is a new paragraph.</p>").insertAfter("button");
  });
});

希望本文所述對大家的jQuery程式設計有所協助。

聯繫我們

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