何时使用< script>标签在html页面的头部和身体部分? [英] When to use the <script> tag in the head and body section of a html page?

查看:78
本文介绍了何时使用< script>标签在html页面的头部和身体部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们必须在< head> 部分使用< script> 标记时,我们必须使用正文部分中的< script> 标记?这两次出现的区别是什么?

When we have to use <script> tag in <head> section and when we have to use the <script> tag in body section? What is the difference between these two occurrences?

推荐答案

中的脚本< head>


调用它们时,或者触发
事件时要执行的脚本都放在函数中。

Scripts to be executed when they are called, or when an event is triggered, are placed in functions.

将您的功能放在head部分,这样它们都在一个
的位置,并且它们不会干扰页面内容。

Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.

中的脚本< body>

如果您没有放入函数内,或者您的脚本写入页面
内容,则应将其放在body部分中。将脚本放在< body> 元素的底部是个好主意
。这可以
改善页面加载,因为脚本编译可以减慢
显示。

If your is not placed inside a function, or if your script writes page content, it should be placed in the body section. It is a good idea to place scripts at the bottom of the <body> element. This can improve page load, because script compilation can slow down the display.

简而言之语言:


  1. 将库等脚本(如jQuery库)放在head部分。

  1. Place library script such as the jQuery library in the head section.

将正常脚本放在头部,除非它成为性能/页面
加载问题。

Place normal script in the head unless it becomes a performance/page load issue.

放置影响脚本
结尾处的页面渲染

Place script that impacts the render of the page at the end of the body

这篇关于何时使用&lt; script&gt;标签在html页面的头部和身体部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆