Javascript HTML <head> 和 <body> 标签有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6303490/
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
What's the difference between HTML <head> and <body> tags?
提问by user531820
What's the difference between HEAD tags and BODY tags?
HEAD 标签和 BODY 标签有什么区别?
most HTML books only 'briefly' mentions <head>
and <body>
tags...but they just go away really fast.
大多数 HTML 书籍仅“简要”提及<head>
和<body>
标记……但它们很快就会消失。
Do they affect how browsers render web pages?
它们会影响浏览器呈现网页的方式吗?
Also, do they affect the order in which javascripts are run?
另外,它们会影响 javascripts 的运行顺序吗?
(I mean, if I have a javascript inside a <head>
tag, would it run BEFORE another javascript inside <body>
tag? Even when <body>
came BEFORE <head>
?)
(我的意思是,如果我在<head>
标签内有一个 javascript ,它会在<body>
标签内的另一个 javascript 之前运行吗?即使<body>
是在 BEFORE 之前<head>
?)
This is too confusing--I haven't ever used a head/body tags, but i never had any trouble with it.
But while reading Jquery tutorial, I saw people recommending putting some codes inside <head>
and the others inside <body>
tags.
这太令人困惑了——我从来没有使用过头部/身体标签,但我从来没有遇到过任何问题。但是在阅读 Jquery 教程时,我看到有人建议将一些代码<head>
放入<body>
标签中,而将其他代码放入标签中。
Thank you!!!
谢谢!!!
采纳答案by azzy81
Generally javascript code will function in the head before code in the body. The head section is usually used to contain information about the page that you dont neccessarily see like the meta keywords meta description or title of a page. You would also link to any external files like .css .js files in the head section as they need to load before the page is displayed.
通常,javascript 代码会在主体代码之前在头部运行。head 部分通常用于包含有关您不需要看到的页面的信息,例如元关键字、元描述或页面标题。您还可以链接到任何外部文件,例如 head 部分中的 .css .js 文件,因为它们需要在页面显示之前加载。
Anything in the body section is what you would expect to be see on screen.
正文部分中的任何内容都是您希望在屏幕上看到的内容。
回答by trutheality
- Things in the head tag are things that shouldn't be rendered: information about the page and how to process it.
- Things in the body tag are the things that should be displayed: the actual content.
- Javascript in the body is executed as it is read and as the page is rendered.
- Javascript in the head is interpreted before anything is rendered.
- head 标签中的内容是不应呈现的内容:有关页面的信息以及如何处理它。
- body 标签中的内容是应该显示的内容:实际内容。
- 正文中的 Javascript 在读取和呈现页面时执行。
- 头部中的 Javascript 在呈现任何内容之前被解释。
回答by Facebook Staff are Complicit
<script>
tags are run when the browser encounters them when loading the page. The <head>
can't contain content for the page, it can only contain meta-information (titles, descriptions, etc), styles and scripts. Therefore if you place a <script>
tag in the <head>
, you are ensuring that it is run before the browser has started loading the content of the page (which must go in the <body>
).
<script>
标签在浏览器加载页面时遇到它们时运行。将<head>
不能包含内容的页面时,它只能包含元信息(标题,描述等),样式和脚本。因此,如果您在 中放置一个<script>
标记<head>
,您就可以确保它在浏览器开始加载页面内容(必须在 中<body>
)之前运行。
If you want to manipulate the content of the page, you need to make sure your script appears after the content you are manipulating. This is why people chose to put scripts at the end of the <body>
.
如果您想操作页面的内容,您需要确保您的脚本出现在您操作的内容之后。这就是为什么人们选择将脚本放在<body>
.
If your code is sloppy (for example, with tags not closed properly), this can cause problems. This is why libraries like jQuery have features to help you run code manipulating the document at the right time.
如果您的代码草率(例如,标签未正确关闭),这可能会导致问题。这就是为什么像 jQuery 这样的库具有帮助您在正确的时间运行操作文档的代码的功能。
回答by user10271863
In my little knowledge:
据我所知:
JavaScript in the head sectionis usually meant to preload certain files(usually procedures or functions as the case may be). For example, a website that utilizes the Time() or Date() function would require that the .js file that contains those functions be called before the website is fully loaded allowing the instance to be available (preloaded) before imminent usage. Same also applies to other custom functions.
head 部分中的 JavaScript通常用于预加载某些文件(通常是过程或函数,视情况而定)。例如,使用 Time() 或 Date() 函数的网站将要求在网站完全加载之前调用包含这些函数的 .js 文件,以允许实例在即将使用之前可用(预加载)。同样也适用于其他自定义函数。
JavaScript in the body sectionis primarily for adding extra functionalityto a website. An example is in the case of a custom .js file where a function is to check for correctness of words in an input string or matching all characters entered in an input string to be of a certain length.
正文部分中的 JavaScript主要用于向网站添加额外功能。一个例子是自定义 .js 文件的情况,其中一个函数用于检查输入字符串中单词的正确性或匹配输入字符串中输入的所有字符为特定长度。
The downside of using either of these two conventions is in calling a custom .js file (meant for website functionality) from the head section. The implication is that the JS file would've already exhausted it's functionality before the website contents are fully loaded.
使用这两个约定中的任何一个的缺点是从 head 部分调用自定义 .js 文件(用于网站功能)。这意味着 JS 文件在网站内容完全加载之前就已经用完了它的功能。
回答by jensgram
A HTML file has headers and a "body" (payload) — just like a HTTP request.
HTML 文件有标题和“正文”(有效负载)——就像 HTTP 请求一样。
The <body>
encapsulates the contents of the document, while the <head>
part contains meta elements, i.e., information aboutthe contents. This is (typically) title, encoding, author, styling etc.
该<body>
封装文件的内容,而<head>
部分包含meta元素,即信息有关的内容。这是(通常)标题、编码、作者、样式等。
As for your question about JavaScript: In general JavaScript is evaluated as it is (loaded and) parsed. So, if you embed JavaScript in the <head>
section it should be parsed immediately.
至于您关于 JavaScript 的问题:一般来说,JavaScript 在被(加载和)解析时被评估。因此,如果您在该<head>
部分中嵌入 JavaScript,则应立即对其进行解析。
回答by Rohan Verma
http://www.w3schools.com/js/js_whereto.asp
http://www.w3schools.com/js/js_whereto.asp
You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time.
It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.
您可以在文档中放置无限数量的脚本,并且可以同时在正文和标题部分中放置脚本。
将所有函数放在 head 部分或页面底部是一种常见的做法。这样,它们都在一个地方,不会干扰页面内容。
The main difference in head and body scripts is that usually people who prefer functions use javascript in the whereas people who prefer inline practices will mostly use it below the document.
head 和 body 脚本的主要区别在于,通常喜欢函数的人在 javascript 中使用 javascript,而喜欢内联实践的人主要在文档下方使用它。
Functional
功能性
<html>
<head>
<script type="text/javascript">
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
<h1>My First Web Page</h1>
<p id="demo"></p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html>
Inline
排队
<html>
<body>
<h1>My First Web Page</h1>
<p id="demo"></p>
<script type="text/javascript">
document.getElementById("demo").innerHTML=Date();
</script>
</body>
</html>
回答by Ben
The browser will process what's in the <head>
to show the <body>
accurately.
浏览器将处理其中的内容<head>
以<body>
准确显示。
The <head>
holds stuff like what character set your page uses, when to refresh, external sheets or scripts you may want to include, and information about your page.
该<head>
喜欢什么字符集,你的页面时刷新,您可能需要外部表或脚本,包括和你的页面信息保存的东西。
The <body>
holds only display-oriented stuff, usually HTML based.
在<body>
只拥有面向显示的东西,通常是基于HTML的。
It's important to keep the model (i.e. the information) and the view (i.e. the HTML) separate. Why? Later, you might want to update a style, and you don't want to chase it down through all of your HTML, each time it happens. Better to do it at one place for the whole document, in the <head>
.
将模型(即信息)和视图(即 HTML)分开是很重要的。为什么?稍后,您可能想要更新样式,并且不想在每次发生时都在所有 HTML 中跟踪它。最好在整个文档的一个地方进行,在<head>
.
回答by Timothy Trousdale
The Head tag is typically used to import other files and define attributes of your page that are not displayed, like meta data. So you could place CSS/Javascript files that you webpage requires in your Head section and they would load before the your page displays.
Head 标签通常用于导入其他文件并定义页面的未显示属性,如元数据。因此,您可以将网页所需的 CSS/Javascript 文件放在 Head 部分,它们会在您的页面显示之前加载。
The body Tag is where you you place the Parts of your website that you want displaed, like p tags, divs, etc.
body 标签是您放置要显示的网站部分的位置,例如 p 标签、div 等。