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

I personally prefer the first syntax

jQuery()

Is this a safe respect for normal use:

$(document).ready()

For another selector I use anyway $('#id')., I just ask for the first.ready

+3
source share
7 answers

Two names $and jQueryare synonyms. jQueryis more explicit; $may be overridden if you use a different infrastructure.

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

, ( , , $):

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

, .

$(document).ready (# 1): , , , DOM . (№ 4) , , jQuery, -, .

+1

jQuery $ ( jQuery):

window.jQuery = window.$ = jQuery

, $. ready():

:

  • $().ready()

  • $(). ready () ( )

  • $()

() API.

+4

jquery script :

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

"" jQuery, , Mootools. jQuery, $.

+2

jQuery, $, , . - , , . .

$(function() {

});

, jQuery noConflict: http://api.jquery.com/jQuery.noConflict/

:

<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 $ can follow here.
  });
  // Code that uses other library $ can follow here.
</script>

, , jQuery - , jQuery() $ jQuery , , .

+2

jQuery , .

0

. ready, :

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

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

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

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

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

Using the jQuery object itself to hook an event readyis a shorthand that is commonly used, but it is less clear just looking at the code what it does.

0
source

All Articles