javascript - $(function() { }) 是否与 $(document).ready(function() { }) 完全相同

标签 javascript jquery

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)

来自.ready() reference .

关于javascript - $(function() { }) 是否与 $(document).ready(function() { }) 完全相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605972/

相关文章:

jquery - Bootstrap col-md-12、col-sm-12 不适用于 bootstraps selectpicker

javascript - 打开弹出窗口时加载代码

javascript - 我如何使用 requirejs 得到真正的错误?

jquery jcache 不会在浏览器刷新后持续存在?

javascript - 使用带有 XML 数据的 jQuery 生成表格

javascript - jquery 将两个数组(键,值)关联成一个数组

javascript - 如何检查数组中是否存在字符串?

javascript - Electron Angular2 PouchDB

javascript - 删除然后将项目添加到 div 会强制页面 View 返回顶部

javascript - 未捕获的语法错误 : Invalid destructuring assignment target