关闭HTML脚本标记 [英] Close HTML Script Tag

查看:82
本文介绍了关闭HTML脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇为什么我的JavaScript无法运行(在Firefox和IE中测试过),如果我这样编写< script> 标签:

 < script type =text / javascriptsrc =script.js/> 

如果我将该行更改为:

 < script type =text / javascriptsrc =script.js>< / script> 

所以我的问题是:为什么我不能关闭脚本标签,因为我没有任何内容。



我的JavaScript代码很简单:

  alert(test); 

注意:我检查了firefox错误控制台并且没有错误。

解决方案

自闭标签的概念是一个XML概念。你不能在HTML中使用它们。 (您可以在XHTML中使用它们,但仅当文档使用XML内容类型时,不需要如果它被作为 text / html )。

在HTML中,一些元素(例如< img> )不能有任何内容,所以它们没有结束标签。



脚本可以具有 src 属性,或脚本可以在元素内,< script> 不是其中之一。
$ b (HTML 5允许 / 字符出现在元素的开始标记的末尾定义为EMPTY,但它对于沉迷于XML的人来说只是糖,并且在语言中没有意义)。


I just curious why my JavaScript can't run (tested in Firefox and IE) if I write the <script> tag like this:

<script type="text/javascript" src="script.js"/>

It will work if I change that line to:

<script type="text/javascript" src="script.js"></script>

So my question is: why I can't close the script tag in the start tag since I don't have any content inside it.

My JavaScript code is simple, just:

alert("test");

Notes: I check firefox error console and no error.

解决方案

The concept of self-closing tags is an XML concept. You can't use them in HTML. (You can use them in XHTML, but only if the document is served with an XML content-type, not if it is served as text/html).

In HTML some elements (such as <img>) cannot have any content, so they don't have end tags).

Since a script can have a src attribute or the script be can inside the element, <script> is not one of them.

(HTML 5 allows a / character to appear at the end of a start tag for an element that is defined as EMPTY, but it is just sugar for people addicted to XML and has no meaning in the language).

这篇关于关闭HTML脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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