document.ready 和 $function 之间的差异
这样做
$(function() {
$("a").click(function(event){
alert("Thanks for visiting!");
});
});
和这
$(document).ready(function(){
$("a").click(function(event){
alert("Thanks for visiting!");
});
});
Possible Duplicate:
What is the difference between these jQuery ready functions?
jquery: Choosing a document.ready method
What is the difference between doing this
$(function() {
$("a").click(function(event){
alert("Thanks for visiting!");
});
});
and this
$(document).ready(function(){
$("a").click(function(event){
alert("Thanks for visiting!");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们是一样的。查看 jQuery
.ready()
文档。这是文档中的引用:They are the same. Check out the jQuery
.ready()
docs. Here's a quote from the docs:您的示例之间的功能没有区别 - 它们都绑定到 DOM 就绪。
作为参考,您可以在两个点绑定 jQuery 代码。
第一个将在 DOM 准备好时执行(两者是等效的):
第二个将在页面完成加载所有图像、样式表等时执行。
当您需要获取
width()
或图像的height()
。仅当映像完全下载到客户端系统后,这些属性才可用。另请注意,
$(window).load(fn);
现已弃用,不应再使用。There is no difference in functionality between your examples - they both bind to DOM ready.
For reference, there are two points at which you can bind your jQuery code.
The first will execute when the DOM is ready (both are equivalent):
The second will execute when the page has finished loading all images, stylesheets etc.
The second is useful when you need to get the
width()
orheight()
of an image. These properties are only available once the image has completely downloaded to the client system.Also note that
$(window).load(fn);
is now deprecated and should no longer be used.以下所有三种语法都是等效的:
http://api.jquery.com/ready/
All three of the following syntaxes are equivalent:
http://api.jquery.com/ready/