Different ways of saying document ready in jQuery?

后端 未结 3 766
时光说笑
时光说笑 2021-01-02 16:59

Are these both the same thing, i.e. ways of saying document ready:

$(function() {
  //
});

and

$(function($) {
   //
})(jQ         


        
3条回答
  •  囚心锁ツ
    2021-01-02 17:20

    This one is incorrect:

    $(function($) {
       //
    })(jQuery);
    

    You're passing a function to $(...), then calling the result. However, the result of $(...) is a jQuery object, which is not a function. You might see it better this way:

    $(
    
        function($) {
            //
        }
    
    )
    
    (jQuery);
    

    Generally, there are three versions of document.ready, which are all equal to each other:

    $(function() {...});
    
    $(document).ready(function() {...});
    
    $().ready(function() {...});
    

提交回复
热议问题