javascript 我把 $(document).ready() 放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6770234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:47:53  来源:igfitidea点击:

Where do I put the $(document).ready()?

javascriptjquery

提问by Matt

I've been trying to add JavaScript to my HTML/CSS, but been running around in circles.

我一直在尝试将 JavaScript 添加到我的 HTML/CSS 中,但一直在兜圈子。

My current set-up is where the html, CSS, and JavaScript files (2 files; my JavaScript code, and jQuery's code) are all separate, but linked to each other via the html page.

我当前的设置是 html、CSS 和 JavaScript 文件(2 个文件;我的 JavaScript 代码和 jQuery 代码)都是独立的,但通过 html 页面相互链接。

So here are my questions:

所以这里是我的问题:

1) Do I put the link to the jQuery code within the html head? Or within my JavaScript code page?

1) 我是否将指向 jQuery 代码的链接放在 html 头中?还是在我的 JavaScript 代码页中?

2) Where does this code go? The html page, or my JavaScript page?

2)这段代码去哪里了?html 页面,还是我的 JavaScript 页面?

$(document).ready(function(){
    //Code here
});

3) Above, by 'code here', they mean JavaScript code, right? Not my html code?

3) 上面的“此处的代码”是指 JavaScript 代码,对吗?不是我的html代码?

4) I've read about initializing JavaScript code at the bottom of an html page. From what I take though, I don't have to do that with jQuery's .ready function, right?

4) 我已经阅读了有关在 html 页面底部初始化 JavaScript 代码的内容。不过,从我的看法来看,我不必用 jQuery 的 .ready 函数来做,对吧?

回答by Seth

  1. You should like to your JavaScript files either in the <head>or above the closing </body>tag.
  2. The code can go anywhere really, but I would suggest an external JavaScript page.
  3. Yes
  4. This is correct.
  1. 您应该将 JavaScript 文件放在<head>结束</body>标记中或结束标记上方。
  2. 代码可以放在任何地方,但我建议使用外部 JavaScript 页面。
  3. 是的
  4. 这是对的。

回答by Elias Peterson

When Javascript code is executing in your browser, all of your included Javascript files and any code you write in-between those "script" tags in the HTML document is going to be executed as though it were all part of one giant file (same namespace). So in some sense, it doesn't matter whether you write your code in the HTML document or whether you write it in an external file that you include - you're free to do either, and it will be executed the same. You can balance maintainability, reusability and convenience (think about what functions you write that you might want to reuse on other pages) and do whichever you feel is best.

当 Javascript 代码在您的浏览器中执行时,您包含的所有 Javascript 文件以及您在 HTML 文档中那些“脚本”标签之间编写的任何代码都将被执行,就好像它们都是一个巨大文件(相同命名空间)的一部分)。因此,从某种意义上说,是在 HTML 文档中编写代码还是在包含的外部文件中编写代码都无关紧要 - 您可以自由选择,并且会以相同的方式执行。您可以平衡可维护性、可重用性和便利性(想想您编写的哪些函数可能希望在其他页面上重用),然后做任何您认为最好的事情。

To make this concrete - this is one valid way to write your Javascript, if you wanted to write the code inside your HTML file:

为了具体化 - 如果您想在 HTML 文件中编写代码,这是编写 Javascript 的一种有效方法:

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript">
      $(document).ready(function(){
        alert('Document Ready!');
      });
    </script>
  </head>
  <body>
  ...

Here's the intro at the jQuery site, for reference: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

这是 jQuery 站点的介绍,供参考:http: //docs.jquery.com/Tutorials: Getting_Started_with_jQuery

Writing your Javascript code at the bottom of the HTML page was/is a technique for getting it to execute as soon as the document is loaded, which is unnecessary when using jQuery's '$(document).ready' (that's what it does - it abstracts the business of getting Javascript functions to execute on page load, and implements it in a cross-browser way).

在 HTML 页面底部编写您的 Javascript 代码曾经/是一种在文档加载后立即执行的技术,这在使用 jQuery 的 '$(document).ready' 时是不必要的(这就是它所做的 - 它抽象了获取 Javascript 函数在页面加载时执行的业务,并以跨浏览器的方式实现它)。

See: Introducing $(document).ready()for more.

请参阅:介绍 $(document).ready()了解更多信息。

回答by hohner

It doesn't really matter where you place your jQuery code. If you place it in the headtag, it'll automatically load everything. If you decide to place it all in an external JavaScript file, you need to link it with a <script type="text/javascript" src="my_file.js"></script>tag.

将 jQuery 代码放在哪里并不重要。如果你把它放在head标签中,它会自动加载所有东西。如果您决定将其全部放在外部 JavaScript 文件中,则需要使用<script type="text/javascript" src="my_file.js"></script>标签链接它。

The 'code here' part is only for JavaScript. What the code is saying is that when the document is ready, run this function. The function can be whatever you like - whatever you put inside the function will run when the document is ready (i.e. when the webpage is called by the browser).

“此处的代码”部分仅适用于 JavaScript。代码的意思是当文档准备好时,运行这个函数。该函数可以是任何你喜欢的——你在函数中放入的任何东西都会在文档准备好时运行(即当浏览器调用网页时)。

You don't need to insert it at the bottom of the HTML page - you can do it anywhere. People only insert it at the bottom to optimize their loading speed. It's nonessential.

您不需要将它插入到 HTML 页面的底部 - 您可以在任何地方进行。人们只在底部插入它以优化他们的加载速度。这是不必要的。

回答by Tomm

$(document).ready(function(){
    //Code here
});

goes in your javascript file. All javascript code that should be executed once the page has loaded goes where the //Code herecomment is.

进入你的 javascript 文件。页面加载后应执行的所有 javascript 代码都放在//Code here注释所在的位置。

Perhaps a quick jQuery tutorialwould be in order?

也许一个快速的jQuery 教程是合适的?

回答by timw4mail

Or, you can put your script tag in the bottom of your body, and not have to use the $(document).ready() function.

或者,您可以将脚本标签放在正文的底部,而不必使用 $(document).ready() 函数。

回答by Fred

  1. Put in the head. This is the most stable way and it works. Some people may disagree and say it is slower, etc, but I have found this to alwayswork.
  2. Where you put your code is up to you. You can put in your headwith a

    <script>Code here</script>

    or in a separate file and include it with

    <script src="reftomyscript.js"></script>

  3. Yes, put your javascript code in this, either in the head or in a separate file.
  4. Yes, and see (1)
  1. 放在头上。这是最稳定的方法,并且有效。有些人可能不同意并说它更慢等,但我发现这总是有效。
  2. 你把代码放在哪里取决于你。你可以把你head

    <script>Code here</script>

    或在一个单独的文件中并将其包含在

    <script src="reftomyscript.js"></script>

  3. 是的,将您的 javascript 代码放在此,无论是在头部还是在单独的文件中。
  4. 是的,见 (1)