Possible Duplicate:
What is the difference between these jQuery ready functions?
Are $(function(){}); and $(“document”).ready(function(){}); the same?
start javascript code with $(function, etc
今天,在查看一些 javascript 代码时,我发现代码的某些部分是用 $(function() { })
编写的。部分以及 $(document).ready(function() { })
内的代码的其他部分一。所以我的第一 react 是:好吧,有什么区别?
经过一番谷歌搜索后,我在 jQuery 教程中找到了下一条语句 Getting Started with jQuery :
The following is a shortcut for the $(document).ready(callback) notation:
$(function() {
// code to execute when the DOM is ready
});
现在的问题是:是 $(function() { })
完全相当于 $(document).ready(function() { })
?
(还有一个“不太全局”的间接问题:将所有代码放在两个部分之一中是否安全?)
最佳答案
是的,$(function() { })
是 $(document).ready(function() { })
的简写。
All three of the following syntaxes are equivalent:
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
关于javascript - $(function() { }) 是否与 $(document).ready(function() { }) 完全相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605972/