javascript 头部和身体之间的脚本元素。[HTML5]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16348140/
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
Script Element Between Head and Body. [HTML5]
提问by pianoman
When validating with W3C, I received the response:
使用 W3C 进行验证时,我收到了响应:
script element between head and body. [HTML5]
头部和身体之间的脚本元素。[HTML5]
How is this specific to HTML5 versus regular XHTML validation?
这对于 HTML5 与常规 XHTML 验证有何不同?
HTML:
HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="services.css" rel="stylesheet" type="text/css">
<script></script>
</head>
<body>
<div id="container">
<div id="header">
</div>
<div id="body">
</div>
<div id="footer">
</div>
</div>
<script></script>
</body>
</html>
回答by Claudio Redi
You placed your scripts on an invalid spot.
您将脚本放在无效的位置。
You should place them within head or body tags. Every time you can, put them at the end of the body. This is to avoid blocking html rendering while dowloading scripts, improving site responsiveness.
您应该将它们放在 head 或 body 标签中。每次可以,将它们放在身体的末端。这是为了避免在下载脚本时阻塞 html 渲染,提高站点响应能力。
From w3.org
从 w3.org
The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.
SCRIPT 元素将脚本放置在文档中。该元素可以在 HTML 文档的 HEAD 或 BODY 中出现任意次数。
回答by Explosion Pills
Just like the message says, this is invalid (not just script tags, but any tags):
正如消息所说,这是无效的(不仅是脚本标签,还有任何标签):
</head>
<tag/>
<body>
Those <script>
tags need to be descendants of either<body>
or <head>
.
这些<script>
标签必须是<body>
或 的后代<head>
。
回答by cortex
As the message suggested, you have some scripts "floating" out head
or body
tags.
正如消息所建议的那样,您有一些脚本“浮动”head
或body
标记。
Move this inside head or body tags:
将其移动到 head 或 body 标签内:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="/fancybox/source/jquery.fancybox.pack.js?v=2.1.4"> </script>
<link rel="stylesheet" type="text/css" href="/fancybox/source/jquery.fancybox.css?v=2.1.4" media="screen">
<script type="text/javascript">
$(document).ready(function() {
$('.fancybox').fancybox();
});
</script>
回答by Keen
Try placing your scripts within the body
tag, at the bottom.
尝试将您的脚本放在body
底部的标记中。