using on() with hover - jQuery

后端 未结 7 1311
感情败类
感情败类 2021-01-26 05:51

This is what I have:

$(\'#blah\').hover(function(){
    $(\'etc\').show();
}, function(){
    $(\'etc\').hide();
});

This works just fine, now

7条回答
  •  半阙折子戏
    2021-01-26 06:05

    From the JQuery source code, hover is not included in the event list that triggered leading to JQuery .on()

    jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
        "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
        "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {
    
        // Handle event binding
        jQuery.fn[ name ] = function( data, fn ) {
            return arguments.length > 0 ?
                this.on( name, null, data, fn ) :
                this.trigger( name );
        };
    });
    

    It is because .hover() is just a shortcut for JQuery .mouseenter() and .mouseleave()

    jQuery.fn.hover = function( fnOver, fnOut ) {
        return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
    };
    

    I hope this brief explanation provides little guidance.

提交回复
热议问题