事件处理程序 - 转义/离开并跳过事件链

时间:2014-12-09 19:33:13

标签: jquery

如果你有几个事件处理程序,那么你怎么能离开/停止链?

$('input').keydown(function(e){
    console.log('first keydown');
});
$('input').keydown(function(e){
    console.log('second keydown');
    // leave/escape the event chain and skip the third keydown
});
$('input').keydown(function(e){
    console.log('third keydown');
});

1 个答案:

答案 0 :(得分:1)

stopImmediatePropagation应该可以解决问题:http://api.jquery.com/event.stopimmediatepropagation/

$('input').keydown(function(e){
    console.log('first keydown');
});
$('input').keydown(function(e){
    console.log('second keydown');
    // leave/escape the event chain and skip the third keydown
    e.stopImmediatePropagation();
});
$('input').keydown(function(e){
    //will not be logged
    console.log('third keydown');
});

jsfiddle:http://jsfiddle.net/8a5zt6qu/1/