简体   繁体   English

jQuery的顶部,底部或两者?

[英]jQuery top, bottom or both?

我正在使用Bootstrap 3框架构建一个简单的网站,其中有一些需要jQuery的项目,我知道jQuery是页面的底部(通过CDN),并且我理解了这样做的原因,但是在本教程中,我正在观看当需要将jQuery放在头部时,我看不到需要两次调用它,或者我只是普通错误。

It's preferred to put the scripts at the bottom just before the closing </body> tag, for example Google suggests this for their analytics scripts: 最好将脚本放在结束</body>标记之前的底部,例如Google 建议将其用于分析脚本:

By placing the script at the end of the page body, you ensure that the tracking code is executed as the last element of the DOM. 通过将脚本放在页面正文的末尾,可以确保将跟踪代码作为DOM的最后一个元素执行。 If a given page load is interrupted for some reason, it is possible that the GIF request for that page view will not be executed. 如果给定的页面加载由于某种原因而中断,则该页面视图的GIF请求可能不会被执行。 However, should you place the tracking code at the top of the page, any load interruptions might result in incomplete or inaccurate reporting anyhow, since the tracking code relies on page data for its reports. 但是,如果将跟踪代码放在页面顶部,则任何负载中断都可能导致报告不完整或不准确,因为跟踪代码依赖于其报告的页面数据。

So that the rest of the page doesn't have to wait for these scripts to load. 这样页面的其余部分就不必等待这些脚本加载。 However, if you notice flickering or you notice that your page's HTML markup looks strange and needs the jquery as soon as possible, you should definitely move it to the <head> portion of your page. 但是,如果您发现闪烁或您注意到页面的HTML标记看起来很奇怪并且需要尽快使用jquery,则绝对应该将其移至页面的<head>部分。 (And don't place the script twice, only at the top or only at the bottom!) (不要将脚本放置在顶部或底部两次!)

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

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