$(document).ready(function(){});$(function(){});jQuery(document).ready(function($){}); 我不确定我是否完全理解#2中发生的事情,以及为什么它等同于标准的方法,#1. jQuery和$实际上是一样的. 如果将函数传递
$(document).ready(function(){});
$(function(){});
jQuery(document).ready(function($){});
我不确定我是否完全理解#2中发生的事情,以及为什么它等同于标准的方法,#1.
jQuery和$实际上是一样的.如果将函数传递给$()函数,jQuery基本上会检查它的类型,如果它是一个函数,它将在DOM准备好时执行它.这只是Javascript:
function myFunc(arg){
if(typeof arg == 'function'){
arg.call();
}
}
来自jQuery源码:
// First, jQuery saves the old values of window.jQuery and window.$
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $in case of overwrite
_$= window.$,
...
// Later on, jQuery returns a reference to the actual jQuery object:
window.jQuery = window.$= jQuery
...
// and if you use noConflict, it'll replace window.$(and window.jQuery) with the old values again
noConflict: function( deep ) {
window.$= _$;
if ( deep ) {
window.jQuery = _jQuery;
}
return jQuery;
}
如果你调用jQuery函数,它将检查参数类型:
init: function( selector, context ) {
...
else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
其中rootjQuery与jQuery(文档)相同
