jQuery 就绪函数别名
我对创建新 jQuery 对象的所有不同方法有点困惑。
相关文档似乎是: http://api.jquery.com/ready/ http://api.jquery.com/jQuery/
从这两个文档中,以下内容都是等效的, (除了别名或不别名“$”之外):
- $(document).ready(handler)
- $().ready(handler)
- $(handler)
- jQuery(function($) {});
- jQuery(文档).ready(function($) {});
这是正确的吗?我错过了什么吗?
I'm a little confused about all the different ways to create a new jQuery object.
the relevent docs seem to be:
http://api.jquery.com/ready/
http://api.jquery.com/jQuery/
From those two docs the following are all equivalent, (with the exception of aliasing or not aliasing '$'):
- $(document).ready(handler)
- $().ready(handler)
- $(handler)
- jQuery(function($) {});
- jQuery(document).ready(function($) {});
Is that correct? Did I miss any?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些在某种程度上是等效的:
$(document).ready(handler)
- 加载 DOM 时调整handler
$().ready(handler)
code> - 加载 DOM 时运行handler
(已弃用,不要使用)$(handler)
- 运行handler
然后加载 DOM -$(document).ready(handler)
的快捷方式jQuery(function($) {})
与上面的 #3 相同,只需使用jQuery
而不是$
别名jQuery(document).ready(function($) {})
- 与第一个相同,再次使用jQuery
而不是$
别名如果
$
被定义为其他内容,例如 Prototype,那么前 3 个将不起作用。最后两个是类似的,它们只是接受传入的第一个参数(jQuery
对象)并将其设置为$
,即使在>$
是别的东西:These are somewhat equivalent:
$(document).ready(handler)
- tuns thehandler
when the DOM is loaded$().ready(handler)
- runs thehandler
when the DOM is loaded (deprecated, don't use)$(handler)
- runs thehandler
then the DOM is loaded - shortcut to$(document).ready(handler)
jQuery(function($) {})
same as #3 above, just usingjQuery
instead of the$
aliasjQuery(document).ready(function($) {})
- same as the first, again usingjQuery
instead of the$
aliasIf
$
is defined as something else, e.g. Prototype, then the first 3 won't work. The last 2 are similiar they're just accepting the first argument passed in (thejQuery
object) and making it$
inside, making it possible to do this even when$
is something else:嗯,还有另一个。来自文档:
其他初始化方法将始终运行...因此您可能会发现自己在许多文件中声明
$(document).ready(function() { //stuff }
,例如,处理程序是 我总是选择
jQuery(document).ready(function($) {})
或$(document).ready(function() {})
通常...我发现它们更具可读性。另一种方法是在结束正文标记之前调用脚本,并在其中执行类似的操作,
如果您需要避免与其他库发生冲突,则使用 $.是一个自动执行的匿名函数,它允许您在其范围内使用别名,而不必担心与其他库发生冲突。
Well, there is another. From the docs:
The other initialiser methods will always run... so you might find yourself declaring
$(document).ready(function() { //stuff }
in a number of files for example, and the handler is always run.I'd go with
jQuery(document).ready(function($) {})
or$(document).ready(function() {})
more often than not... I find that they're more readable.Another approach would be to call a script just before the closing body tag and in it do something like,
if you need to avoid conflicts with other libraries using $. This is a self-executing anonymous function and it lets you use the alias in its scope without fear of conflicts from other libraries.
如果您只使用 jQuery,那么 $() 相当于 jQuery()。所以这涵盖了其中的一半。
然后,如果您使用 $() 而不是 $(document).ready,则它们是相同的。在这种情况下,它只是一个辅助函数。例如,您可能想在其他东西上添加准备,在这种情况下,您会这样做: $(foo).load({})
最后,我不知道 $().ready 是什么意思,因为您必须传递一个参数。
Well if you are only using jQuery then $() is equivalent to jQuery(). So that covers half of them.
Then, if you use $() instead of $(document).ready, those are the same. In this case, it is just a helper function. You may want to add ready on something else for example, in which case, you would do: $(foo).load({})
Last, I don't know what you mean with $().ready because you have to pass a parameter.