在jQuery中说文档的不同方式? [英] Different ways of saying document ready in jQuery?
本文介绍了在jQuery中说文档的不同方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两者是否相同,即说准备文件的方式:
Are these both the same thing, i.e. ways of saying document ready:
$(function() {
//
});
和
$(function($) {
//
})(jQuery);
或两者之间是否存在差异,如果是,那么何时应该使用哪个?
or is there a difference between the two, if so then when should I use which?
推荐答案
第一个是 .ready()
的快捷方式。
当你试图调用一个不可调用的对象时,第二个就是无效。
The second one is simply invalid as you're trying to call a non-callable object.
你可能意味着这个:
// v--------no $ at the beginning
(function( $ ) {
// simply a new lexical environment with a
// local $ parameter pointing to jQuery
})(jQuery);
...虽然它与DOM准备无关。
...though it has nothing to do with DOM ready.
你的第一个例子中有一个变体将两者结合起来:
There is a variation on your first example that combines the two:
jQuery(function( $ ) {
// DOM ready, and creates a local $ parameter pointing to jQuery
});
这篇关于在jQuery中说文档的不同方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文