jQuery ready()的四个变体-有什么区别? [英] Four variations of jQuery ready() -- what's the difference?
问题描述
我已经看到四种不同的方法来告诉jQuery在文档准备好后执行一个函数.这些都是等价的吗?
I've seen four different ways to tell jQuery to execute a function when the document is ready. Are these all equivalent?
$(document).ready(function () {
alert('$(document).ready()');
});
$().ready(function () {
alert('$().ready()');
});
$(function () {
alert('$()');
});
jQuery(function ($) {
alert('jQuery()');
});
推荐答案
没有区别.
$
与jQuery
相同.如果您查看未缩小的来源,则会看到var $ = jQuery = ...
或类似的内容.
$
is the same as jQuery
. If you view the unminified source, you will see var $ = jQuery = ...
or something to that effect.
jQuery
函数检查其参数的类型,如果是函数,则将其与$(document).ready(...)
一样对待
The jQuery
function checks the type of it's parameter, if it is a function, it treats it the same as $(document).ready(...)
不带参数调用jQuery
默认情况下使用document
.因此$()
和$(document)
是相同的.在Firebug中尝试.
Calling jQuery
without a parameter defaults to using document
. So $()
and $(document)
are identical. Try it in Firebug.
这篇关于jQuery ready()的四个变体-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!