$(document).ready的不同形式 [英] Different forms of $(document).ready
问题描述
我看到人们在写作
$(document).ready(function(){
});
和一些文字
$(function() {
});
有什么区别,什么时候使用什么?
What's the difference and when to use what?
推荐答案
$
是jQuery对象本身,在调用时实现了一大堆不同的接口. $('string')
运行选择器或构造一个节点; $(domElement)
包装一个元素...,而$(a_function)
是$(document).ready(a_function)
的便捷缩写.有关更多信息,请参见 jQuery API文档.
$
is the jQuery object itself, which when called implements a whole pile of different interfaces. $('string')
runs a selector or constructs a node; $(domElement)
wraps an element... and $(a_function)
is a convenient short hand for $(document).ready(a_function)
. See the jQuery API docs for (much) more information.
附带的注释:$(function () { ... })
短一些[em> ,但是如果您想搜索所有准备就绪的事件,则可能希望使用.ready
来搜索:)
A note in passing: $(function () { ... })
is shorter, but if you ever want to search for all of your on-ready events, you might be wishing that you had .ready
to search for :)
这篇关于$(document).ready的不同形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!