3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
.bind()與.on()的區(qū)別:
(1)是否支持selector這個(gè)參數(shù)值。由于javascript的事件冒泡特性,如果在父元素上注冊(cè)了一個(gè)事件處理函數(shù),當(dāng)子元素上發(fā)生這個(gè)事件的時(shí)候,父元素上的事件處理函數(shù)也會(huì)被觸發(fā)。
如果使用on的時(shí)候,不設(shè)置selector,那么on與bind就沒有區(qū)別了。
(2)on綁定的事件處理函數(shù),對(duì)于未來(lái)新增的元素一樣可以的,和delegate效果相同,而bind則不行。
(3) delegate用法與on()相同,只是參數(shù)的順序不同:
擴(kuò)展資料:
.bind()與.on()的實(shí)際應(yīng)用:
1.bind()是直接綁定在元素上 ,將一本地地址與一套接口捆綁。如無(wú)錯(cuò)誤發(fā)生,則bind()返回0。否則的話,將返回-1,應(yīng)用程序可通過WSAGetLastError()獲取相應(yīng)錯(cuò)誤代碼。
用于事件處理程序
function ClassName(){
this.eventHandler = (function(){
}).bind(this);
}
2.on()則實(shí)現(xiàn)事件代理, 可以在匹配元素上綁定一個(gè)或者多個(gè)事件處理函數(shù)。
(1) 用來(lái)綁定多事件,并且為同一函數(shù),如:
$('div').on('click mouseover',function(){
//do sth
});
(2)多個(gè)事件綁定不同函數(shù),如:
$('div').on({
'click':function(){
//do sth
},
'mouseover':function(){
//do sth
}
});
(3)事件代理,如:
html:
<button id="bt1">按鈕1</button>
jq:
$('#bt1').on('click',function(){
$('body').append('<button>按鈕2</button>');
});
$('body').on('click','.bt2',function(){
console.log('這是bt2');
}

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
.bind()是直接綁定在元素上
.live()則是通過冒泡的方式來(lái)綁定到元素上的。更適合列表類型的,綁定到document DOM節(jié)點(diǎn)上。和.bind()的優(yōu)勢(shì)是支持動(dòng)態(tài)數(shù)據(jù)。
.delegate()則是更精確的小范圍使用事件代理,性能優(yōu)于.live()
.on()則是最新的1.9版本整合了之前的三種方式的新事件綁定機(jī)制

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
1、在on()方法介紹的與bind()方法的區(qū)別:As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see.bind(), .delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()2、在bind()方法介紹的與on()方法的區(qū)別:As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.總結(jié)整理上述兩段英文的區(qū)別意思為:bind()函數(shù)是jQuery 1.7之前或更早版本采用的一個(gè)用來(lái)綁定事件處理程序的函數(shù);on()函數(shù)是jQuery 1.7版本提供的首選的用來(lái)綁定事件處理程序的函數(shù);從1.7版本的介紹以及參數(shù)描述來(lái)看,其實(shí)這兩個(gè)函數(shù)基本上用法一致,但可能在早期的版本中,bind()函數(shù)一次只能為標(biāo)簽對(duì)象綁定一個(gè)事件的處理程序,而on()函數(shù)則可以一次為多個(gè)不同的事件綁定處理程序。
添加回答
舉報(bào)