jquery临时解除绑定事件 [英] jquery temporary unbinding events

查看:223
本文介绍了jquery临时解除绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我完全错过了关于甚至在jQuery中处理的事情,但这是我的问题。

maybe I'm totally missing something about even handling in jQuery, but here's my problem.

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

Let's assume there are some event binding, like

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

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

Now, I'd like to introduce a new mousemove binding that doesn't override the previous one, but temporarily exclude (unbind) it. In other words, when I bind my function, I must be sure that no other functions will ever execute for that event, until I restore them.

我正在寻找类似于:

$(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不在我的控制之下,否则这将是无用的:)

Of course, the somefunc is not under my control, or this would be useless :)

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

Is my understanding that is possible to bind multiple functions to the same event, and that the execution of those functions can't be pre-determined. I'm aware of stopping event propagation and immediate event propagation, but I'm thinking that they are useless in my case, as the execution order can't be determined (but maybe I'm getting these wrong).

我该怎么做?
提前付款
~Aki

How can I do that? Thanks in advance ~Aki

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

I need to highlight this: I need that the previously installed handler (somefunc) isn't executed. I am NOT defining that handler, it may be or may be not present, but its installed by a third-party user.

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

Ok, this is not feasible right now, I think I'm needing the eventListenerList, which is not implemented in most browsers yet. http://www.w3.org/TR/2002/WD-DOM-Level-3-Events-20020208/changes.html

推荐答案

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

Another way could be to use custom events, something along these lines:

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/

Highly annoying demo here: http://jsfiddle.net/SkFvW/

这篇关于jquery临时解除绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆