<脚本>标签必须包含单独的标签?脚本>
今天,我在进行一些测试时注意到,关闭 标记的方式要么成功,要么破坏我的页面。例如,这有效:
<script src="scripts/jquery.js" type="text/javascript"></script>
但这不起作用:
<script src="scripts/jquery.js" type="text/javascript" />
当我使用 IE 的开发人员工具时,该文件似乎会显示,但似乎它只是被忽略了。有谁见过这种情况或知道为什么会发生这种情况?提前致谢!
I noticed today while doing some testing that the way I close my <script>
tag either makes or breaks my page. For example, this works:
<script src="scripts/jquery.js" type="text/javascript"></script>
but this does not:
<script src="scripts/jquery.js" type="text/javascript" />
The file appears to show up when I use IE's Developer Tools, but it seems like it just gets ignored. Has anyone ever seen this or know why it might be happening? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须包含结束脚本标记。即使您只包含外部脚本,脚本元素也不会自动关闭。
You must include a closing script tag. The script element is not self closing, even when you're only including an external script.
标记只能在真正有效的 XHTML 文档中自闭合 - 即使用
Content-Type
ofapplication/xhtml+xml
– 在支持的浏览器中查看时的和(IE8 不符合条件;IE9+ 符合条件)。在所有其他 HTML 文档中(无论声明什么
DOCTYPE
),标记都不是自闭合的,因此 < strong>必须用
关闭。
在这个非常详细的答案中阅读更多内容。
The
<script>
tag can only be self-closing in truly valid XHTML documents – that is, a XHTML page served with theContent-Type
ofapplication/xhtml+xml
– and when viewed in a supporting browser (IE8 does not qualify; IE9+ does).In all other HTML documents, (regardless of what
DOCTYPE
is declared), the<script>
tag is not self-closing and therefore must be closed with a</script>
.Read more in this very detailed answer.
我还注意到您始终需要
。这可能是因为它需要标签之间的内容(“”计数),即使您使用的是
src
。I have also noticed you always need the
</script>
. It's probably because it requires content between the tags ("" counts), even though you're usingsrc
.