這隻能確保沒有其他click
事件監聽器附加。因此,您的處理程序將成爲唯一一個點擊#myButton
作用的處理程序,編輯: as @ T.J。 Crowder在評論中指出,unbind()
不會影響以不同方式連接的處理程序,而不會調用bind()
。
這也是值得注意的,即:
如jQuery的1.7,則。對()和.off的()方法是優選的附加和上元件移除事件處理程序
(摘自jQuery.unbind() doc)。實際上,unbind()
is implemented internally由off()
方法)。
考慮:
$("#myButton").bind('click', function() {
console.log('click 1');
});
$("#myButton").unbind('click').bind('click', function() {
console.log('click 2');
});
// When clicked, prints "click 2"
對戰:
$("#myButton").bind('click', function() {
console.log('click 1');
});
// note: no unbind here
$("#myButton").bind('click', function() {
console.log('click 2');
});
// When clicked, prints "clicked 1" followed by "clicked 2"
請參閱:http://stackoverflow.com/questions/1264353/what-does-bind-and-unbind-mean-in-jquery –
從jQuery1.7開始['on()'](http://建議使用api.jquery.com/bind/)。你的代碼將是'$(「#myButton」)。關閉()。在()'。 – bhb