jQuery - Using .one() with hover

前端 未结 2 1371
孤街浪徒
孤街浪徒 2021-01-12 04:29

Is there any way to get a hover function to only execute once? This is what I\'m currently trying:

$(\'#ask\').live(\'hover\', function() {

    $(\'#homesea         


        
2条回答
  •  失恋的感觉
    2021-01-12 05:09

    You've already answered yourself, use .one() (not .live()).

    But as lasseespeholt just pointed out in a comment, .hover() is shorthand for binding to mouseenter and mouseleave, and is not an event in itself.

    Try this:

    $('#ask').one('mouseenter', function() {
    
        $('#homesearch-after').hide(300);
    
        $.doTimeout( 300, function() {
            hideClosedSearchLink();
            showHomeSearch();
        });
    
    });
    

    If that still doesn't work, try just using the good ol' .hover() and then .unbind()ing it immediately after it's finished.

    $('#ask').hover(function() {
    
        $('#homesearch-after').hide(300);
    
        $.doTimeout( 300, function() {
            hideClosedSearchLink();
            showHomeSearch();
        });
    
        // Again taking into account what lasseespeholt said
        $(this).unbind('mouseenter mouseleave')
    
    });
    

提交回复
热议问题