如何覆盖jquery事件处理程序

时间:2021-11-05 00:05:32

I'll try to explain the problem with a simple code.

我将尝试用简单的代码来解释这个问题。

var fireClick = function() { alert('Wuala!!!') };

$('#clickme').click(fireclick);
$('#clickme').click(fireclick);

So now it will obviously alert 2 times but i need it alert only once. Tried so many ways, but no result.

现在它显然会提醒2次但我只需要提醒1次。尝试了很多方法,但没有结果。

Thanks.

谢谢。

5 个解决方案

#1


122  

As of jQuery 1.7 you should be using off to remove event handlers and on to add them, though you can still use the click shorthand.

在jQuery 1.7中,您应该使用off来删除事件处理程序并添加它们,不过您仍然可以使用click快捷方式。

$('#clickme').off('click').on('click', fireclick);
$('#clickme').off().on('click', fireclick);

Original answer:

最初的回答:

If you want to replace all click handlers, call unbind first without a function argument. If you want to replace all event handlers, don't specify the event type.

如果您想替换所有的单击处理程序,请首先调用unbind而不使用函数参数。如果要替换所有事件处理程序,请不要指定事件类型。

$('#clickme').unbind('click').click(fireclick);
$('#clickme').unbind().click(fireclick);

#2


21  

Use a namespace to make sure you don't remove any other listeners:

使用名称空间确保不删除任何其他侦听器:

$('#clickme').off('click.XXX').on('click.XXX', fireclick);

As long as no other code uses XXX, you can be sure that you have not messed up some other behaviour that you weren't aware of.

只要没有其他代码使用XXX,您就可以确保没有搞乱其他一些您不知道的行为。

#3


18  

You may use the jQuery function unbind to remove the first event:

您可以使用jQuery函数unbind来删除第一个事件:

var fireClick = function() { alert('Wuala!!!') };

$('#clickme').click(fireclick);
$('#clickme').unbind('click', fireClick); // fireClick won't fire anymore...
$('#clickme').click(fireclick); // ...but now it will

#4


3  

I would try to eliminate the extra calls, but short of tyhat you could make sure to call both of these each time:

我会试着消除额外的电话,但是你可以确保每次都打电话给他们:

$('#clickme').unbind('click', fireclick);
$('#clickme').click(fireclick);

#5


-8  

You should use setInterval. The problem is that you cannot have two alerts pop at the same time. First one has to close first or the second one can't appear on screen...

您应该使用setInterval。问题是您不能同时弹出两个警报。第一个必须先关闭,否则第二个就不能出现在屏幕上……

#1


122  

As of jQuery 1.7 you should be using off to remove event handlers and on to add them, though you can still use the click shorthand.

在jQuery 1.7中,您应该使用off来删除事件处理程序并添加它们,不过您仍然可以使用click快捷方式。

$('#clickme').off('click').on('click', fireclick);
$('#clickme').off().on('click', fireclick);

Original answer:

最初的回答:

If you want to replace all click handlers, call unbind first without a function argument. If you want to replace all event handlers, don't specify the event type.

如果您想替换所有的单击处理程序,请首先调用unbind而不使用函数参数。如果要替换所有事件处理程序,请不要指定事件类型。

$('#clickme').unbind('click').click(fireclick);
$('#clickme').unbind().click(fireclick);

#2


21  

Use a namespace to make sure you don't remove any other listeners:

使用名称空间确保不删除任何其他侦听器:

$('#clickme').off('click.XXX').on('click.XXX', fireclick);

As long as no other code uses XXX, you can be sure that you have not messed up some other behaviour that you weren't aware of.

只要没有其他代码使用XXX,您就可以确保没有搞乱其他一些您不知道的行为。

#3


18  

You may use the jQuery function unbind to remove the first event:

您可以使用jQuery函数unbind来删除第一个事件:

var fireClick = function() { alert('Wuala!!!') };

$('#clickme').click(fireclick);
$('#clickme').unbind('click', fireClick); // fireClick won't fire anymore...
$('#clickme').click(fireclick); // ...but now it will

#4


3  

I would try to eliminate the extra calls, but short of tyhat you could make sure to call both of these each time:

我会试着消除额外的电话,但是你可以确保每次都打电话给他们:

$('#clickme').unbind('click', fireclick);
$('#clickme').click(fireclick);

#5


-8  

You should use setInterval. The problem is that you cannot have two alerts pop at the same time. First one has to close first or the second one can't appear on screen...

您应该使用setInterval。问题是您不能同时弹出两个警报。第一个必须先关闭,否则第二个就不能出现在屏幕上……