取消jQuery事件处理 [英] Cancel jQuery event handling
问题描述
element.onclick = function(){/ * code * /}
想象一下,使用jQuery方法bind()或类似的处理程序设置事件处理程序。 >
$('element')。bind('click',function(){/ *另一个函数* /})
如何从最初描述的处理程序中阻止使用jQuery调用处理程序?
NB stopPropagation()等jQuery的方法从该函数不起作用,因为它是通过本机事件对象传递的。
我不是100%肯定你在问什么,但也许这将有助于:
你可以创建一个新的事件对象(符合W3C DOM)通过jQuery的暴露事件构造函数:
例如:
element.onclick = function(e){
var aBetterEventObject = jQuery.Event(e);
//现在你可以做你想要的:(跨浏览器)
aBetterEventObject.preventDefault()
aBetterEventObject.isDefaultPrevented()
aBetterEventObject.stopPropagation()
aBetterEventObject.isPropagationStopped()
aBetterEventObject.stopImmediatePropagation()
aBetterEventObject.isImmediatePropagationStopped()
}
编辑:再次阅读您的问题,我不认为传播是问题 - 您似乎想要取消事件处理程序在事件中运行处理程序 - 我不知道这是可能的。你可以解除所有的处理程序( jQuery(elem).unbind('click')),但我不认为这是你以后...
I have setup onclick event handler in the following manner:
element.onclick = function() { /*code */ }
Imagine there are event handlers setup using jQuery method bind() or similar handlers.
$('element').bind('click', function(){/*another function*/})
How can I prevent invoking handler defined with jQuery from the handler I have described in the beginning?
NB stopPropagation() and etc. jQuery's methods doesn't work from that function, because it is passed with native event object.
I'm not 100% sure what you're asking but maybe this will help:
You can create a new event object (compliant with W3C DOM) via jQuery's exposed Event constructor:
For example:
element.onclick = function(e) {
var aBetterEventObject = jQuery.Event(e);
// Now you can do what you want: (Cross-browser)
aBetterEventObject.preventDefault()
aBetterEventObject.isDefaultPrevented()
aBetterEventObject.stopPropagation()
aBetterEventObject.isPropagationStopped()
aBetterEventObject.stopImmediatePropagation()
aBetterEventObject.isImmediatePropagationStopped()
}
EDIT: Reading through your question again, I don't think propagation is the problem - you seem to want to cancel an event handler from running within an event handler - I'm not sure this is possible. You could just unbind all handlers (jQuery(elem).unbind('click')) but I don't think that's what you're after...
这篇关于取消jQuery事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!