Different ways of saying document ready in jQuery?

后端 未结 3 783
时光说笑
时光说笑 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:19

    The first one is a shortcut for .ready().

    The second one is simply invalid as you're trying to call a non-callable object.

    You probably meant this:

    // v--------no $ at the beginning
        (function( $ ) {
    
           // simply a new lexical environment with a 
           //        local $ parameter pointing to jQuery
    
        })(jQuery);
    

    ...though it has nothing to do with DOM ready.

    There is a variation on your first example that combines the two:

    jQuery(function( $ ) {
    
      // DOM ready, and creates a local $ parameter pointing to jQuery
    
    });
    

提交回复
热议问题