What is the difference between these jQuery ready functions?

后端 未结 9 1429
名媛妹妹
名媛妹妹 2020-11-22 15:31

what is difference between

$(function(){

}); 

and

$(document).ready(function() { 

});
相关标签:
9条回答
  • 2020-11-22 16:01

    I use $(function() {}); because it's shorter. As far as I know there is no difference between the two ways of doing it.

    0 讨论(0)
  • 2020-11-22 16:07

    Nothing whatsoever.

    This function behaves just like $(document).ready(), in that it should be used to wrap other $()

    You can see this in the source code:

    rootjQuery = jQuery(document);
    
    ...
    
    } else if ( jQuery.isFunction( selector ) ) {
        return rootjQuery.ready( selector );
    }
    
    0 讨论(0)
  • 2020-11-22 16:07

    The two are exactly equivalent: use whichever form you like.

    That said, I personally always use the expanded form $(document).ready(function(){}); for the simple reason that it is completely obvious what the code is doing. The approximate idea is that of "self-documenting code". Anyone coming to the code later on will immediately see that the code is to be run on the document's ready event. With the short-hand form, you have to rely upon the reader of your code understanding the meaning.

    0 讨论(0)
提交回复
热议问题