使用jQuery,這是更好,性能代價:jQuery性能:使用每個按鈕的選擇器或使用一個選擇器和檢查類?
添加事件處理程序爲每個按鈕:
$(".toolbar .add").click() {
// Add stuff here
}
$(".toolbar .remove").click() {
// Remove stuff here
}
$(".toolbar .clear").click() {
// Clear stuff here
}
或添加一個事件處理程序,並檢查類(或其他相關屬性),看看哪些採取的行動:
$(".toolbar .button").click() {
if($(this).hasClass("add"))
// Add stuff here
} else if($(this).hasClass("remove")) {
// Remove stuff here
} else if($(this).hasClass("clear")) {
// Clear stuff here
}
}
或者還有更好的辦法嗎?
(如果這有什麼差別,我實際使用delegate
,不bind
在我的應用程序)
在我的情況下,我不得不使用'委託',因爲工具欄加載了Ajax。 – Chaim