从jQuery的API文档的网站为ready
以下所有三种语法都是等效的:
- $(文件).就绪(句柄)
- $().ready(处理程序)(不建议这样做)
- $(句柄)
做完作业后 - 阅读和播放源代码,我不知道为什么
$().ready(handler)
Run Code Online (Sandbox Code Playgroud)
不推荐.第一种和第三种方式完全相同,第三种方法在缓存的jQuery对象上调用ready函数document:
rootjQuery = jQuery(document);
...
...
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
Run Code Online (Sandbox Code Playgroud)
但是ready函数没有与所选节点元素的选择器交互,ready源代码:
ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
// Add the callback
readyList.add( fn );
return this;
},
Run Code Online (Sandbox Code Playgroud)
如您所见,它只是将回调添加到内部队列(readyList)并且不会更改或使用集合中的元素.这使您可以ready在每个jQuery对象上调用该函数.
喜欢:
$('a').ready(handler) DEMO$('fdhjhjkdafdsjkjriohfjdnfj').ready(handler) …