在 jQuery 中删除事件处理程序的最佳方法?
我有一个输入类型=“图像”
。 这就像 Microsoft Excel 中的单元格注释。 如果有人在与此 input-image
配对的文本框中输入数字,我会为 input-image
设置一个事件处理程序。 然后,当用户单击图像
时,他们会看到一个小弹出窗口,以便向数据添加一些注释。
我的问题是,当用户在文本框中输入零时,我需要禁用 input-image
的事件处理程序。 我已尝试以下方法,但无济于事。
$('#myimage').click(function { return false; });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
jQuery ≥ 1.7
从 jQuery 1.7 开始,事件 API 已更新,
.bind()
/.unbind()
仍然可用于向后兼容,但首选方法是使用on()/off() 函数。 下面现在是jQuery < 1.7
在您的示例代码中,您只是向图像添加另一个单击事件,而不是覆盖前一个事件:
然后两个单击事件都会被触发。
正如人们所说,您可以使用取消绑定来删除所有点击事件:
如果您想添加单个事件然后删除它(而不删除可能已添加的任何其他事件),那么您可以使用事件命名空间:
并仅删除您的事件:
jQuery ≥ 1.7
With jQuery 1.7 onward the event API has been updated,
.bind()
/.unbind()
are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be,jQuery < 1.7
In your example code you are simply adding another click event to the image, not overriding the previous one:
Both click events will then get fired.
As people have said you can use unbind to remove all click events:
If you want to add a single event and then remove it (without removing any others that might have been added) then you can use event namespacing:
and to remove just your event:
回答此问题时此功能不可用,但您也可以使用
live()
方法来启用/禁用事件。
此代码将发生的情况是,当您单击
#mydisablebutton
时,它会将禁用的类添加到#myimage
元素中。 这将使选择器不再与元素匹配,并且在删除“disabled”类以使.live()
选择器再次有效之前,事件不会被触发。通过添加基于该类的样式还有其他好处。
This wasn't available when this question was answered, but you can also use the
live()
method to enable/disable events.What will happen with this code is that when you click
#mydisablebutton
, it will add the class disabled to the#myimage
element. This will make it so that the selector no longer matches the element and the event will not be fired until the 'disabled' class is removed making the.live()
selector valid again.This has other benefits by adding styling based on that class as well.
这可以通过使用取消绑定功能来完成。
您可以将多个事件处理程序添加到 jquery 中的同一对象和事件。 这意味着添加新的并不会取代旧的。
有多种更改事件处理程序的策略,例如事件命名空间。 在线文档中有一些关于此的页面。
看看这个问题(这就是我学会解除绑定的方式)。 答案中对这些策略有一些有用的描述。
如何在 jquery 中读取绑定的悬停回调函数
This can be done by using the unbind function.
You can add multiple event handlers to the same object and event in jquery. This means adding a new one doesn't replace the old ones.
There are several strategies for changing event handlers, such as event namespaces. There are some pages about this in the online docs.
Look at this question (that's how I learned of unbind). There is some useful description of these strategies in the answers.
How to read bound hover callback functions in jquery
如果您想仅响应一次事件,那么以下语法应该非常有用:
使用arguments.callee,我们可以确保一个特定的匿名函数处理程序被删除,因此对于给定事件有一个时间处理程序。 希望这对其他人有帮助。
If you want to respond to an event just one time, the following syntax should be really helpful:
Using arguments.callee, we can ensure that the one specific anonymous-function handler is removed, and thus, have a single time handler for a given event. Hope this helps others.
也许解除绑定方法适合你
maybe the unbind method will work for you
我必须使用 prop 和 attr 将事件设置为 null。 我无法用其中之一做到这一点。 我也无法让 .unbind 工作。 我正在研究 TD 元素。
I had to set the event to null using the prop and the attr. I couldn't do it with one or the other. I also could not get .unbind to work. I am working on a TD element.
如果事件以这种方式附加,并且目标要取消附加:
If event is attached this way, and the target is to be unattached:
要
删除
全部事件处理程序
,这对我有用:删除所有事件处理程序意味着拥有简单的
HTML结构
没有附加到元素
及其子节点
的所有事件处理程序
。 为此,jQuery 的 clone()
提供了帮助。这样,我们将用
克隆
来代替原始
,并且上面没有事件处理程序
。祝你好运...
To
remove
ALLevent-handlers
, this is what worked for me:To remove all event handlers mean to have the plain
HTML structure
without all theevent handlers
attached to theelement
and itschild nodes
. To do this,jQuery's clone()
helped.With this, we'll have the
clone
in place of theoriginal
with noevent-handlers
on it.Good Luck...
如果您使用 $(document).on() 向动态创建的元素添加侦听器,那么您可能必须使用以下命令将其删除:
If you use
$(document).on()
to add a listener to a dynamically created element then you may have to use the following to remove it:您可能将
onclick
处理程序添加为内联标记:如果是这样,jquery
.off()
或.unbind()
将不起作用。 您还需要在 jquery 中添加原始事件处理程序:然后 jquery 具有对事件处理程序的引用,并且可以将其删除:
VoidKing 在上面的评论中更间接地提到了这一点。
You may be adding the
onclick
handler as inline markup:If so, the jquery
.off()
or.unbind()
won't work. You need to add the original event handler in jquery as well:Then the jquery has a reference to the event handler and can remove it:
VoidKing mentions this a little more obliquely in a comment above.
2014 年更新
使用最新版本的 jQuery,您现在只需执行
$( "#foo" ).off( ".myNamespace" ) 即可取消绑定命名空间上的所有事件;
Updated for 2014
Using the latest version of jQuery, you're now able to unbind all events on a namespace by simply doing
$( "#foo" ).off( ".myNamespace" );
删除内联 onclick 事件的最佳方法是
$(element).prop('onclick', null);
Best way to remove inline onclick event is
$(element).prop('onclick', null);
感谢您的信息。 非常有用,我用它来锁定其他用户在编辑模式下的页面交互。 我将它与 ajaxComplete 结合使用。 行为不一定相同,但有些相似。
Thanks for the information. very helpful i used it for locking page interaction while in edit mode by another user. I used it in conjunction with ajaxComplete. Not necesarily the same behavior but somewhat similar.
如果
.on()
方法之前与特定选择器一起使用,如下例所示:unbind()
和.off()
方法不会起作用。但是, .undelegate() 方法可用于从事件中完全删除所有匹配元素的处理程序当前选择器:
In case
.on()
method was previously used with particular selector, like in the following example:Both
unbind()
and.off()
methods are not going to work.However, .undelegate() method could be used to completely remove handler from the event for all elements which match the current selector:
我知道这来得很晚,但为什么不使用纯 JS 来删除该事件呢?
或者,如果您使用命名函数作为事件处理程序:
I know this comes in late, but why not use plain JS to remove the event?
or, if you use a named function as an event handler:
这也很好用。简单易行。参见 http://jsfiddle.net/uZc8w/570/
This also works fine .Simple and easy.see http://jsfiddle.net/uZc8w/570/
如果您通过
html
设置onclick
,则需要removeAttr ($(this).removeAttr('onclick'))
如果您通过jquery(如上面示例中的第一次单击后),那么您需要
unbind($(this).unbind('click'))
if you set the
onclick
viahtml
you need toremoveAttr ($(this).removeAttr('onclick'))
if you set it via jquery (as the after the first click in my examples above) then you need to
unbind($(this).unbind('click'))
所描述的所有方法都不适合我,因为我将带有
on()
的单击事件添加到在运行时创建元素的文档中:我的解决方法:
因为我不能取消绑定“.button”类 我刚刚为具有相同 CSS 样式的按钮分配了另一个类。 通过这样做,实时/事件处理程序最终忽略了点击:
希望有帮助。
All the approaches described did not work for me because I was adding the click event with
on()
to the document where the element was created at run-time:My workaround:
As I could not unbind the ".button" class I just assigned another class to the button that had the same CSS styles. By doing so the live/on-event-handler ignored the click finally:
Hope that helps.
希望我的下面的代码解释了一切。
HTML:
Hope my below code explains all.
HTML:
我今天在工作中遇到了一个与此相关的有趣案例,其中有一个 $(window) 的滚动事件处理程序。
但是,要撤销该事件处理程序,我们不能只使用它,
因为在这个非常常见的目标上可能还有其他滚动事件处理程序,而且我对通过关闭所有功能来管理其他功能(已知或未知)不感兴趣。滚动处理程序。
我的解决方案是首先将处理程序功能抽象为命名函数,然后在事件侦听器设置中使用它。
然后,有条件地,当我们需要撤销它时,我这样做了:
卡顿部分是第二个参数,它冗余地选择原始选择器。 但是, .off() 的 jquery 文档仅提供了一种方法签名,用于指定要删除的处理程序,这要求这个中间参数是
我还没有冒险使用
null
或''
作为第二个参数来测试它,但也许冗余的$window
不是必要的。I had an interesting case relevant to this come up at work today where there was a scroll event handler for $(window).
But, to revoke that event handler, we can't just use
because there are likely other scroll event handlers on this very common target, and I'm not interested in hosing that other functionality (known or unknown) by turning off all of the scroll handlers.
My solution was to first abstract the handler functionality into a named function, and use that in the event listener setup.
And then, conditionally, when we need to revoke that, I did this:
The janky part is the 2nd parameter, which is redundantly selecting the original selector. But, the jquery documentation for .off() only provides one method signature for specifying the handler to remove, which requires this middle parameter to be
I haven't ventured to test it out with a
null
or''
as the 2nd parameter, but perhaps the redundant$window
isn't necessary.