Revision 83c91b8b-9bfe-48ba-869e-19b74a2e880a - Stack Overflow

In #1, your function won't be called until the DOM is ready; passing a function into `$()` is a shortcut for [`ready`][1].

In #2, you're never calling your function at all. The idiom you've probably seen there is:

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

That's fairly standard thing you'd do when you want the code to run immediately (not wait for DOM ready), and you want to use `$` within the function to refer to jQuery, but you want to be compatible with [`noConflict`][2] mode. You see this a lot in plug-ins. It defines a function accepting a `$` argument, which shadows any `$` defined globally, and then immediately calls that function passing in `jQuery` as the argument. So even if `$` doesn't map to `jQuery` globally, it does within that function. You can do the same thing like this:

 (function() {
 var $ = jQuery;
 })();

...but for some reason the earlier example is more common, even if a bit less clear. (It's a couple of characters shorter, I suppose.)

 [1]: http://api.jquery.com/ready/
 [2]: http://api.jquery.com/jQuery.noConflict/

AltStyle によって変換されたページ (->オリジナル) /