我有两个函数在两个不同的时间绑定(bind)到单击事件(使用 jQuery)。它们被解雇的顺序很重要。他们按照正确的顺序开火。问题是,当第一个函数返回 false 时,第二个函数仍在触发!
如何正确取消事件?
示例代码:
$(document).click(function() {
alert('a');
return false;
});
$(document).click(function() {
alert('b');
});
单击该页面时,您仍然会看到“b”警报消息。这是 Not Acceptable !
最佳答案
使用stopImmediatePropagation
jQuery 事件对象的功能。
Keeps the rest of the handlers from being executed. This method also stops the bubbling by calling event.stopPropagation().
$(document).click(function(event) {
alert('a');
event.stopImmediatePropagation();
return false;
});
$(document).click(function() {
alert('b');
});
关于jQuery 多个事件处理程序 - 如何取消?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/652495/