How do I unbind “hover” in jQuery?

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

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

This does not work:

$(this).unbind(\'hover\');
8条回答
  •  日久生厌
    2020-11-28 03:35

    All hover is doing behind the scenes is binding to the mouseover and mouseout property. I would bind and unbind your functions from those events individually.

    For example, say you have the following html:

    Link
    

    then your jQuery would be:

    $(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);
      });
    
    });
    

提交回复
热议问题