如何在jQuery中取消绑定"悬停"?

zsh*_*arp 102 javascript jquery

如何在jQuery中取消绑定"悬停"?

这不起作用:

$(this).unbind('hover');
Run Code Online (Sandbox Code Playgroud)

Cre*_*esh 208

$(this).unbind('mouseenter').unbind('mouseleave')

或者更简洁(感谢@Chad Grant):

$(this).unbind('mouseenter mouseleave')

  • 或$(this).unbind('mouseenter mouseleave') (38认同)

Phi*_*ler 65

实际上,jQuery文档比上面显示的链接示例有更简单的方法(尽管它们可以正常工作):

$("#myElement").unbind('mouseenter mouseleave');
Run Code Online (Sandbox Code Playgroud)

从jQuery 1.7开始,您还可以使用$.on()$.off()进行事件绑定,因此要取消绑定悬停事件,您将使用更简单和更整洁:

$('#myElement').off('hover');
Run Code Online (Sandbox Code Playgroud)

伪事件名称"悬停" 用作 "mouseenter mouseleave" 的简写,但在早期的jQuery版本中处理不同; 要求您明确删除每个文字事件名称.$.off()现在使用允许您使用相同的速记删除两个鼠标事件.

编辑2016:

仍然是一个受欢迎的问题,所以值得注意@ Dennis98在下面的评论中的观点,在jQuery 1.9+中,"悬停"事件被弃用,支持标准的"mouseenter mouseleave"调用.所以你的事件绑定声明现在应该是这样的:

$('#myElement').off('mouseenter mouseleave');

  • 我有jQuery 1.10.2和`$ .off("悬停")`不起作用.但是,使用这两个事件效果很好. (4认同)

tva*_*son 10

单独取消绑定mouseentermouseleave事件,或取消绑定元素上的所有事件.

$(this).unbind('mouseenter').unbind('mouseleave');
Run Code Online (Sandbox Code Playgroud)

要么

$(this).unbind();  // assuming you have no other handlers you want to keep
Run Code Online (Sandbox Code Playgroud)