jquery - head.ready() 与 $(document).ready

标签 jquery document-ready head.js

最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。

来自 headjs.com:

The “DOM ready” event such as $(document).ready() is already fired when the scripts arrive. If the loaded scripts depend on that event make sure your library can handle this. jQuery 1.4+ works.

考虑到这一点,如果 $(document).ready() 中的代码依赖于加载 head.js 的外部脚本,那么设置使用 jQuery 的页面的最佳方法是什么?

我们是否可以完全丢失 $(document).ready() 调用,并且仍然成功设置依赖于文档准备就绪的事件监听器之类的东西?例如:

head.js("script1.js", "script2.js", "script3.js", function() {
    $('#button').click(function(event) {
        alert("clicked");
    });
});

或者我们想将 $(document).ready() 包装在函数中吗?

只是想知道确保一切在需要时准备就绪的最佳实践是什么。

最佳答案

无论哪种方式都可以。处理程序传递到 ready()如果 DOM 已经完全初始化,则立即调用。

为了获得性能提升,您可能需要删除ready处理程序并直接包含您的代码,除非您依赖像jQuery的这样的副作用>$ 对象被传递给处理程序。

关于jquery - head.ready() 与 $(document).ready,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221122/

相关文章:

javascript - 应该在标记中使用 Head.js 还是在 </body> 之前使用缩小脚本和脚本?

javascript - 如何在 Magento 中使用像 head.js 或 labjs 这样的 JavaScript 加载器

javascript - 为现有值添加跨度

jquery - 将 jquery 中的 click() 替换为 document.ready()

javascript - tinymce加载后执行js函数

javascript - Angular 服务/值没有从 $(document).ready() 中获取新的 var

css - Head.js 屏幕尺寸与 CSS3 @media 查询 - 为什么是前者?

jQuery历史插件错误: $.历史未定义

javascript - 在网站上实现 twitch Stream

javascript - 动态设置 iframe 高度;不适用于 ie 和 chrome