jQuery中on()方法用法實例詳解
本文實例分析了jQuery on()方法的用法。分享給大家供大家參考。具體分析如下:
一、jQuery on()方法的使用:
on(events,[selector],[data],fn)
events:一個或多個用空格分隔的事件類型和可選的命名空間,如"click"或"keydown.myPlugin" 。
selector:一個選擇器字符串用于過濾器的觸發(fā)事件的選擇器元素的后代。如果選擇器為null或省略,當它到達選定的元素,事件總是觸發(fā)。
data:當一個事件被觸發(fā)時要傳遞event.data給事件處理函數(shù)。
fn:該事件被觸發(fā)時執(zhí)行的函數(shù)。 false 值也可以做一個函數(shù)的簡寫,返回false。
二、jQuery on()方法的優(yōu)點:
1、提供了一種統(tǒng)一綁定事件的方法
2、仍然提供了.delegate()的優(yōu)點,當然如果需要你也可以直接用.bind()
三、與.bind(), .live(), .delegate()的比較:
1、其實.bind(), .live(), .delegate()都是通過.on()來實現(xiàn)的
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()了,它已經(jīng)不再被推薦了,而且還有許多問題
4、.delegate()會提供很好的方法來提高效率,同時我們可以添加一事件處理方法到動態(tài)添加的元素上。
5、我們可以用.on()來代替上述的3種方法
四、jQuery on()方法的使用示例
1、綁定click事件,使用off()方法移除on()所綁定的方法
$("p").on("click",function(){
$(this).css("background-color","pink");
});
$("button").click(function(){
$("p").off("click");
});
});
2、多個事件綁定同一個函數(shù)
$("p").on("mouseover mouseout",function(){
$("p").toggleClass("intro");
});
});
3、多個事件綁定不同函數(shù)
$("p").on({
mouseover:function(){$("body").css("background-color","lightgray");},
mouseout:function(){$("body").css("background-color","lightblue");},
click:function(){$("body").css("background-color","yellow");}
});
});
4、綁定自定義事件
$("p").on("myOwnEvent", function(event, showName){
$(this).text(showName + "! What a beautiful name!").show();
});
$("button").click(function(){
$("p").trigger("myOwnEvent",["Anja"]);
});
});
5、傳遞數(shù)據(jù)到函數(shù)
{
alert(event.data.msg);
}
$(document).ready(function(){
$("p").on("click", {msg: "You just clicked me!"}, handlerName)
});
6、適用于未創(chuàng)建的元素
$("div").on("click","p",function(){
$(this).slideToggle();
});
$("button").click(function(){
$("<p>This is a new paragraph.</p>").insertAfter("button");
});
});
希望本文所述對大家的jQuery程序設計有所幫助。
- jQuery on()方法綁定動態(tài)元素的點擊事件實例代碼淺析
- jQuery on()綁定動態(tài)元素出現(xiàn)的問題小結
- 解決jquery中動態(tài)新增的元素節(jié)點無法觸發(fā)事件問題的兩種方法
- jquery append 動態(tài)添加的元素事件on 不起作用的解決方案
- jQuery給動態(tài)添加的元素綁定事件的方法
- jQuery動態(tài)創(chuàng)建html元素的常用方法匯總
- jQuery獲取動態(tài)生成的元素示例
- jquery動態(tài)添加元素事件失效問題解決方法
- jQuery事件綁定on()、bind()與delegate() 方法詳解
- Jquery中offset()和position()的區(qū)別分析
- jQuery on()方法綁定動態(tài)元素的點擊事件無響應的解決辦法
相關文章
關于jquery input textare 事件綁定及用法學習
目前1.7以上,jquery的事件綁定已經(jīng)用on替換了原來的bind,接下來為大家介紹下bind的使用方法及input textare事件,感興趣的朋友可以參考下哈2013-04-04jQuery的animate函數(shù)實現(xiàn)圖文切換動畫效果
animate()在jquery中作為一個方法,可用于創(chuàng)建動畫效果,jquery中的animate()方法讓那個頁面增加了很好的視覺效果2015-05-05