$(selector).bind(event,data,function) $(selector).live(event,data,function)//jquery1.9版本如下支持,jquery1.9及其以上版本刪除了此方法,jquery1.9以上版本用on()方法來代替 $(selector).delegate(childSelector,event,data,function)//jquery1.4.2及其以上版本; $(selector).on(event,childselector,data,function)//jquery1.7及其以上版本;jquery1.7版本出現以後用於替代bind(),live()綁定事件方式;
event:必需項;添加到元素的一個或多個事件,例如 click,dblclick等;jquery
單事件處理:例如 $(selector).bind("click",data,function);瀏覽器
多事件處理:1.利用空格分隔多事件,例如 $(selector).bind("click dbclick mouseout",data,function);函數
2.利用大括號靈活定義多事件,例如 $(selector).bind({event1:function, event2:function, ...}) 性能
3.空格相隔方式:綁定較爲死板,不能給事件單獨綁定函數,適合處理多個事件調用同一函數狀況;spa
大括號替代方式:綁定較爲靈活,能夠給事件單獨綁定函數; 代理
data:可選;須要傳遞的參數;code
$('a').bind('click', function() { alert("That tickles!") });
當咱們在a 上面點擊的時候,首先會觸發它自己所綁定的click事件,而後會一路往上,觸發它的父元素,祖先元素上全部綁定的click事件。orm
/* The .bind() method attaches the event handler directly to the DOM element in question ( "#members li a" ). The .click() method is just a shorthand way to write the .bind() method. */ $( "#members li a" ).bind( "click", function( e ) {} ); $( "#members li a" ).click( function( e ) {} );
.click(), .hover()...這些很是方便的事件綁定,都是bind的一種簡化處理方式。對於利用ID選出來的元素是很是好的,不單單是很快的能夠hook上去(由於一個頁面只有一個id),並且當事件發生時,handler能夠當即被執行(相對於後面的live, delegate)實現方式xml
二、.live()則是經過冒泡的方式來綁定到元素上的。更適合列表類型的,綁定到document DOM節點上。一旦事件冒泡到document上,jQuery將會查找selector/event metadata,而後決定那個handler應該被調用。當handler在執行的時候,由於有冒泡的參與,確實會有一些延遲,可是綁定的時候是特別的快。和.bind()相比的時候有一個好處就是咱們不須要在每一個元素上再去綁定事件,而只在document上綁定一次就能夠了。儘管這個不是最快的方式,可是確實是最少浪費的。blog
優勢:
缺點:
三、.delegate()則是更精確的小範圍使用事件代理,性能優於.live()。它不會把全部的event所有綁定到document,而是由你決定把它放在哪兒。而和.live()相同的地方在於都是用event delegation.
缺點: