Difference between jQuery’s .hide() and setting CSS to display: none

后端 未结 7 1621
天涯浪人
天涯浪人 2020-11-30 23:19

Which am I better off doing? .hide() is quicker than writing out .css(\"display\", \"none\"), but what’s the difference and what are both of them a

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 23:42

    Looking at the jQuery code, this is what happens:

    hide: function( speed, easing, callback ) {
        if ( speed || speed === 0 ) {
            return this.animate( genFx("hide", 3), speed, easing, callback);
    
        } else {
            for ( var i = 0, j = this.length; i < j; i++ ) {
                var display = jQuery.css( this[i], "display" );
    
                if ( display !== "none" ) {
                    jQuery.data( this[i], "olddisplay", display );
                }
            }
    
            // Set the display of the elements in a second loop
            // to avoid the constant reflow
            for ( i = 0; i < j; i++ ) {
                this[i].style.display = "none";
            }
    
            return this;
        }
    },
    

提交回复
热议问题