jQuery $(document).ready 有必要吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4643990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is $(document).ready necessary?
提问by Joshua Robison
I saw this question in stackoverflow but do not feel that it was answered at all.
我在 stackoverflow 中看到了这个问题,但根本不觉得它得到了回答。
Is $(document).ready
necessary?
有$(document).ready
必要吗?
I link all my javascripts at the bottom of the page so in theory they are all running after the document is ready anyways.
我在页面底部链接了我所有的 javascripts,所以理论上它们都在文档准备就绪后运行。
回答by zzzzBov
Is
$(document).ready
necessary?
有
$(document).ready
必要吗?
no
不
if you've placed all your scripts right before the </body>
closing tag, you've done the exact same thing.
如果您将所有脚本都放在</body>
结束标记之前,那么您所做的就是完全相同的事情。
Additionally, if the script doesn't need to access the DOM, it won't matter where it's loaded beyond possible dependencies on other scripts.
此外,如果脚本不需要访问 DOM,那么除了可能依赖于其他脚本之外,它被加载到哪里都无关紧要。
For many CMS's, you don't have much choice of where the scripts get loaded, so it's good form for modular code to use the document.ready
event. Do you really want to go back and debug old code if you reuse it elsewhere?
对于许多 CMS,您没有太多选择脚本加载的位置,因此模块化代码使用document.ready
事件是一种很好的形式。如果您在其他地方重用旧代码,您真的想回去调试旧代码吗?
off-topic:
题外话:
As a side note: you should use jQuery(function($){...});
instead of $(document).ready(function(){...});
as it forces the alias to $
.
附带说明:您应该使用jQuery(function($){...});
而不是$(document).ready(function(){...});
因为它强制别名为$
.
回答by turtlepick
No, if your javascript is the last thing before close you won't need to add those tags.
不,如果您的 javascript 是关闭前的最后一件事,您将不需要添加这些标签。
As a side note, a shorthand for $(document).ready is the code below.
作为旁注,$(document).ready 的简写是下面的代码。
$(function() {
// do something on document ready
});
This question might be good. Did you read it? jQuery: Why use document.ready if external JS at bottom of page?
这个问题可能很好。你读过它吗? jQuery:如果页面底部有外部 JS,为什么要使用 document.ready?
回答by BradChesney79
No, it isn't necessary provided you know you do not have any deferred stuff happening-- and in most cases you will know if you have developed what you are working on from top to bottom.
不,只要您知道没有任何延迟的事情发生,就没有必要——而且在大多数情况下,您会知道是否已经从上到下开发了您正在处理的内容。
--It is when you bring in someone else's code, without thoroughly auditing it, that you don't know.
--当你引入别人的代码时,没有彻底审核它,你不知道。
So, ask yourself are you using a framework or editor that helps you with the structure? Are you bringing in someone else's code and you haven't bothered to read through each file? Are you prepared to go through the Operating System, Browser, and Browser Version matrix of testing your code? Do you needto squeeze every single ounce of speed from your code?
那么,问问自己,您使用的是帮助您构建结构的框架或编辑器吗?您是否引入了其他人的代码而没有费心阅读每个文件?您准备好通过操作系统、浏览器和浏览器版本矩阵测试您的代码了吗?您是否需要从代码中榨取每一盎司的速度?
document.ready() makes many of those question become irrelevant. document.ready() was designed to make your life easier. It comes at a small (and I dare say acceptable) performance hit.
document.ready() 使许多问题变得无关紧要。document.ready() 旨在让您的生活更轻松。它带来了很小(我敢说可以接受)的性能损失。
回答by Krishna Chytanya
I have seen references/blog post across the internet regarding the usage of jquery's document.ready
. In my opinion both using it or putting all your javascript at the bottom of the page are both valid. And now the question would be which would be better? It is just a matter of programming style.
我在互联网上看到过关于 jquery 的document.ready
. 在我看来,使用它或将所有 javascript 放在页面底部都是有效的。现在的问题是哪个更好?这只是编程风格的问题。
回答by mbokil
No, it is not necessary. You can either put the script tag right before the body closing tag or if you are supporting IE9+ you can use native JS.
不,没有必要。您可以将脚本标记放在正文结束标记之前,或者如果您支持 IE9+,则可以使用本机 JS。
<script>alert('DOM Loaded!');</script>
</body>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
// DOM has loaded ready to fire JS scripts
});
</script>