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


107

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

这不起作用:

$(this).unbind('hover');

2
您是要取消绑定分配给悬停事件的功能,还是要修改<a> </a>悬停?
贾斯汀·尼斯纳

为了澄清Justin Niessner的问题,您是否要删除Javascript / DOM事件或CSS声明?后者是一个更复杂的问题。
2009年

Answers:


214

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

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

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


42
或$(this).unbind('mouseenter mouseleave')
乍得Grant

mouseleave之后,mouseenter是否需要必要的顺序?
sanghavi7 2012年

70

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

$("#myElement").unbind('mouseenter mouseleave');

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

$('#myElement').off('hover');

伪事件名称“ hover” 用作 “ mouseenter mouseleave” 的简写,但在早期的jQuery版本中的处理方式有所不同;要求您明确删除每个文字事件名称。$.off()现在使用可以让您使用相同的速记来删除两个鼠标事件。

编辑2016年:

仍然是一个受欢迎的问题,因此值得在下面的注释中引起@ Dennis98的注意,即在jQuery 1.9+中,不推荐使用 “悬停”事件,而推荐使用标准的“ mouseenter mouseleave”调用。因此,您的事件绑定声明现在应如下所示:

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


4
我有jQuery 1.10.2,$.off("hover")但无法正常工作。但是,同时使用两个事件效果很好。
Alexis Wilke 2014年

值得一提的是,当给出多个过滤参数时,提供的所有参数都必须匹配才能删除事件处理程序。我还注意到,jQuery API文档指出.off()方法将删除与.on()关联的事件处理程序。我不确定这是否意味着它仅适用于使用.on()添加的事件。但这不应该。
Phil.Wheeler

@AlexisWilke是的,它已在v1.9中删除,请查找最后一个链接。;)
Dennis98 '16

1
@ Dennis98-您只是在谈论将jQuery悬停黑客转移到deprecated.js,对吧?$ .off()事件绑定在更高版本的jQuery中仍然可用。
Phil.Wheeler '16

对。:) $.off()仍然存在,这是当前取消绑定事件的推荐方法。因此,到目前为止,您需要写$(element).off("mouseenter mouseleave");
Dennis98'7

10

分别取消绑定mouseentermouseleave事件,或取消绑定元素上的所有事件。

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

要么

$(this).unbind();  // assuming you have no other handlers you want to keep

4

unbind()不适用于硬编码的嵌入式事件。

因此,例如,如果您想从中取消绑定mouseover事件 <div id="some_div" onmouseover="do_something();">,我发现这$('#some_div').attr('onmouseover','')是一种快速而肮脏的方法。


4

另一个解决方案是.die()用于与.live()关联的事件。

例如:

// attach click event for <a> tags
$('a').live('click', function(){});

// deattach click event from <a> tags
$('a').die('click');

您可以在此处找到很好的参考:探索jQuery .live()和.die()

(对不起,我的英语:“>)


2

悬停在后台执行的所有操作都绑定到mouseover和mouseout属性。我会分别从这些事件绑定和取消绑定您的函数。

例如,假设您有以下html:

<a href="#" class="myLink">Link</a>

那么您的jQuery将是:

$(document).ready(function() {

  function mouseOver()
  {
    $(this).css('color', 'red');
  }
  function mouseOut()
  {
    $(this).css('color', 'blue');
  }

  // either of these might work
  $('.myLink').hover(mouseOver, mouseOut); 
  $('.myLink').mouseover(mouseOver).mouseout(mouseOut); 
  // otherwise use this
  $('.myLink').bind('mouseover', mouseOver).bind('mouseout', mouseOut);


  // then to unbind
  $('.myLink').click(function(e) {
    e.preventDefault();
    $('.myLink').unbind('mouseover', mouseOver).unbind('mouseout', mouseOut);
  });

});

更正,在查看jQuery src之后,悬停实际上绑定到mouseenter / mouseleave。您应该做同样的事情。
bentewey

2

您可以删除通过连接一个特定的事件处理程序on,使用off

$("#ID").on ("eventName", additionalCss, handlerFunction);

// to remove the specific handler
$("#ID").off ("eventName", additionalCss, handlerFunction);

使用此方法,您将仅删除handlerFunction
另一个好的做法是为多个附加事件设置一个nameSpace

$("#ID").on ("eventName1.nameSpace", additionalCss, handlerFunction1);
$("#ID").on ("eventName2.nameSpace", additionalCss, handlerFunction2);
// ...
$("#ID").on ("eventNameN.nameSpace", additionalCss, handlerFunctionN);

// and to remove handlerFunction from 1 to N, just use this
$("#ID").off(".nameSpace");

0

我发现这是.hover()的第二个参数(函数)

$('#yourId').hover(
    function(){
        // Your code goes here
    },
    function(){
        $(this).unbind()
    }
});

第一个函数(.hover()的参数)是鼠标悬停,它将执行您的代码。第二个参数是mouseout,它将与#yourId解除绑定悬停事件。您的代码将只执行一次。


1
这样的$.unbind()本身不会从该对象中删除所有事件吗?在这种情况下,您的$.click()事件之类的事情现在会失败,对吗?
Alexis Wilke 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.