简体   繁体   English

我还需要$(document).ready(function(){})吗?

[英]Do I still need $(document).ready(function(){ })?

I've been developing in javascript for a few months and I have been using $(document).ready(function(){ at the beginning of all my scripts. I've noticed other folks don't use this in their scripts but I can't seem to get mine working without it. I'd like to keep my code cleaner, but for all my google searches I can't seem to figure out how to get rid of it. 我已经用javascript开发了几个月而且我一直在使用$(document).ready(function(){在我所有脚本的开头。我注意到其他人不会在他们的脚本中使用它但是如果没有它,我似乎无法让我的工作。我想保持我的代码更清洁,但对于我所有的谷歌搜索,我似乎无法弄清楚如何摆脱它。

I know it tells the browser to execute my script as soon as the page loads but is there something global I can set so that I don't need to explicitly tell the browser to execute each of my scripts when the page loads? 我知道它会告诉浏览器在页面加载后立即执行我的脚本,但是我可以设置全局,这样我就不需要在页面加载时明确告诉浏览器执行我的每个脚本吗? Or is it a more global problem with where in my html files the scripts are located? 或者在我的html文件中脚本所在的位置是一个更全局的问题?

You're needing document.ready probably because you're interacting with the DOM before it loads. 你需要document.ready可能是因为你在加载之前与DOM交互。 How can the script manipulate elements that are not there yet? 脚本如何操作尚不存在的元素?

If you stick your script at the end of the file you will not need it. 如果您将脚本粘贴在文件的末尾,则不需要它。 It's also good practice to do so for a lot of Javascript files as they can take time to process (especially if they're hosted externally). 对于许多Javascript文件来说这也是一种很好的做法,因为它们可能需要一些时间来处理(特别是如果它们是在外部托管的)。 Putting them at the end of the file often speeds up the page load time . 将它们放在文件的末尾通常会加快页面加载时间

All $(document).ready(function() { ... }); 全部$(document).ready(function() { ... }); or $(function() { ... }); $(function() { ... }); does is wait for the document to be ready for manipulation. 是等待文档准备好进行操作。 You should use $(document).ready(function() { ... }); 你应该使用$(document).ready(function() { ... }); or $(function() { ... }); $(function() { ... }); if your scripts are inline or in the <head /> section because JavaScript executes in the order in which it appears on the page otherwise. 如果您的脚本是内联的或在<head />部分,因为JavaScript按照它在页面上显示的顺序执行,否则。

To do away with $(document).ready(function() { ... }); 废除$(document).ready(function() { ... }); or $(function() { ... }); $(function() { ... }); , simply move your scripts down to the bottom of the page after all of your content, right before the closing </body> tag. ,只需在结束</body>标记之前将所有内容移动到页面底部。

Putting the scripts at the bottom is really a best practice anyway. 无论如何,将脚本置于底部确实是最佳实践。 For this and other best practices, I recommend you take a look at Html5Boilerplate and Yahoo! 对于这个和其他最佳实践,我建议你看一下Html5BoilerplateYahoo! Best Practices . 最佳实践

the $(document).ready() convention is part of jQuery, not just JavaScript. $(document).ready()约定是jQuery的一部分,而不仅仅是JavaScript。 From their' documentation on the ready function : 从他们关于就绪功能文档

This is the first thing to learn about jQuery: If you want an event to work on your page, you should call it inside the $(document).ready() function. 这是了解jQuery的第一件事:如果你想让一个事件在你的页面上运行,你应该在$(document).ready()函数中调用它。 Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded. 在加载DOM之后和加载页面内容之前,其中的所有内容都将加载。

So yes, it is required. 所以是的,这是必需的。 jQuery does come with a shorthand for this though, so you could do the following: jQuery确实附带了一个简写,所以你可以做到以下几点:

$(function() {
  //jquery code
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM