How do I unbind “hover” in jQuery?

前端 未结 8 1998
醉话见心
醉话见心 2020-11-28 02:54

How do I unbind \"hover\" in jQuery?

This does not work:

$(this).unbind(\'hover\');
8条回答
  •  难免孤独
    2020-11-28 03:47

    Actually, the jQuery documentation has a more simple approach than the chained examples shown above (although they'll work just fine):

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

    As of jQuery 1.7, you are also able use $.on() and $.off() for event binding, so to unbind the hover event, you would use the simpler and tidier:

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

    The pseudo-event-name "hover" is used as a shorthand for "mouseenter mouseleave" but was handled differently in earlier jQuery versions; requiring you to expressly remove each of the literal event names. Using $.off() now allows you to drop both mouse events using the same shorthand.

    Edit 2016:

    Still a popular question so it's worth drawing attention to @Dennis98's point in the comments below that in jQuery 1.9+, the "hover" event was deprecated in favour of the standard "mouseenter mouseleave" calls. So your event binding declaration should now look like this:

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

提交回复
热议问题