我有一个元素,上面定义了一个事件处理程序:
$('.element').click(function(){
alert('I was clicked');
})
有没有办法添加 - 或者更好的EXTEND - 为click
选择器.element
事件定义的回调以获得类似的内容?
/* In another part of my .js script I add these lines of code.
* Here is where I'd like to "add" or "extend" the handler with another callback */
$('.element.specific_extended_element').click(function(){
alert('Extended click');
/* -- execute parent handler -- */
})
我想获得的结果类似于点击.element.specific_extended_element
时打开的两个提醒:
1st alert: "Extended click";
2nd alert: "I was clicked".
答案 0 :(得分:2)
一种简单直接的方法是在您想要重复使用功能时执行您经常执行的操作:将其设置为可以从多个位置调用的功能:
// The original handler
function originalHandler() {
alert('I was clicked');
}
// Hooking it up, not including the special element
$('.element:not(.specific_extended_element)').click(originalHandler);
// Hooking up your additional handler
$('.element.specific_extended_element').click(function(e){
alert('Extended click');
originalHandler.call(this, e);
});