简体   繁体   中英

using jQuery() for $(document).ready()?

personally I prefer the first syntax

jQuery()

is it safe respect to the common used:

$(document).ready()

For the other selector i will anyway use $('#id'). i am just asking for the first .ready

jQuery and $ are interchangeable (from jQuery source):

window.jQuery = window.$ = jQuery

but be careful with other libraries that might use $ as well. As for using ready() :

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)

  • $().ready(handler) (this is not recommended)

  • $(handler)

From ready() API.

Right after you loaded jquery script add this code:

(function($){
    $.fn.oldReady = $.fn.ready;
    $.fn.ready = function(fn){
        return $.fn.oldReady( function(){ try{ if(fn) fn.apply($,arguments); } catch(e){}} );
    }
})(jQuery);

Its "safer" to use jQuery , as it won't conflict with other frameworks such as Mootools. If you only use jQuery, there is no harm in using $ .

If you're only using the jQuery framework, then I'd go with the $ syntax, in particular the short-hand version for the ready event handler as being cleaner. This is only a personal preference -- it's immediately obvious to me what it does and it's not as verbose. You can choose any of the other forms.

$(function() {

});

If you have other frameworks, then you should follow the instructions to set up jQuery in noConflict mode: http://api.jquery.com/jQuery.noConflict/

Code sample from the referenced page:

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {  // or jQuery(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

If you're building your own plugin and you can't be sure that jQuery is the only framework in use then, I'd use jQuery() internally or at least assign $ to the jQuery object explicitly within your scope so that you know what you're working with.

The two names $ and jQuery are synonyms. jQuery is the more explicit; $ may be overridden if you use another framework.

If you pass a function as the first argument to your call to the jQuery function (or, obviously, to $ ), it is executed exactly as if it were a call to jQuery(document).ready . So yes, it is exactly the same.

Indeed, all these are functionally equivalent (provided you don't have anything else mucking around with $ ):

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

It is entirely a matter of circumstance and style as to which you use.

My personal preference is to use the explicit $(document).ready call (#1): it is obvious that this is code that will be run when the DOM is ready. The major advantage of your preference (#4) is that it clearly denotes that the code is jQuery, which may be useful for someone reading your code in future.

jQuery文档说他们是一样的。

Your question is really two in one. There are several ways of hooking up the ready event, but these four variations are relevant to the question:

jQuery(function(){ ... });

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

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

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

The $ identifier is an alias for the jQuery identifier. Using the noConflict method you could free up the $ alias if it conflicts with any other library and only use the jQuery identifier (or even free up all identifiers and specify one of your own).

Using the jQuery object itself for hooking up the ready event is a shorthand that is commonly used, but it's less clear from just looking at the code what it does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM