Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery将事件处理程序添加到已定义事件处理程序的元素_Jquery_Callback_Event Handling - Fatal编程技术网

jQuery将事件处理程序添加到已定义事件处理程序的元素

jQuery将事件处理程序添加到已定义事件处理程序的元素,jquery,callback,event-handling,Jquery,Callback,Event Handling,我有一个定义了事件处理程序的元素: $('.element').click(function(){ alert('I was clicked'); }) 是否有一种方法可以为.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 hand

我有一个定义了事件处理程序的元素:

$('.element').click(function(){
    alert('I was clicked');
})
是否有一种方法可以为
.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时打开的两个警报。单击特定的\u extended\u element

1st alert: "Extended click";
2nd alert: "I was clicked".

一种简单而直接的方法是,当您希望重用功能时,执行您经常执行的操作:使其成为您可以从多个位置调用的功能:

// 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);
});