jquery临时解除绑定事件

也许我完全错过了甚至在jQuery中处理的东西,但这是我的问题。

我们假设有一些事件绑定,比如

$(element).bind("mousemove", somefunc); 

现在,我想介绍一个新的mousemove绑定,它不会覆盖前一个,但暂时排除(解除绑定)它。 换句话说,当我绑定我的函数时,我必须确保没有其他函数将执行该事件,直到我恢复它们。

我正在寻找类似的东西:

 $(element).bind("mousemove", somefunc); // Somefunc is used regularly var savedBinding = $(element).getCurrentBinding("mousemove"); $(element).unbind("mousemove").bind("mousemove", myfunc); // Use myfunc instead $(element).unbind("mousemove", myfunc).bind("mousemove", savedBindings); 

当然,somefunc不在我的控制之下,否则这将是无用的:)

我的理解是可以将多个函数绑定到同一个事件,并且无法预先确定这些函数的执行。 我知道停止事件传播和立即事件传播,但我认为它们在我的情况下是无用的,因为执行顺序无法确定(但也许我得到这些错误)。

我怎样才能做到这一点? 在此先感谢~Aki

编辑:我需要强调这一点:我需要不执行以前安装的处理程序(somefunc)。 我没有定义该处理程序,它可能是或可能不存在,但它由第三方用户安装。

EDIT2:好的,现在这是不可行的,我想我需要eventListenerList,它还没有在大多数浏览器中实现。 http://www.w3.org/TR/2002/WD-DOM-Level-3-Events-20020208/changes.html

另一种方法可能是使用自定义事件,这些内容如下:

 var flag = 0; $(element).bind("mousemove", function() { if(flag) { $(this).trigger("supermousemove"); } else { $(this).trigger("magicmousemove"); } }).bind("supermousemove", function() { // do something super }).bind("magicmousemove", function() { // do something magical }); $("#foo").click(function() { flag = flag == 1 ? 0 : 1; // simple switch }); 

这里非常烦人的演示: http : //jsfiddle.net/SkFvW/

如果事件绑定到多个元素,那就好了:

 $('.foo').click(function() { if ( ! $(this).hasClass('flag')) { do something } }); 

(将类’flag’添加到unbind中,将其添加到’bind’)