只有这样才能识别:
1
只有这样才能识别:
1

首页 / 知识

关于javascript:为什么自关闭脚本元素不起作用?

2023-04-15 18:50:00

关于javascript:为什么自关闭脚本元素不起作用?

Why don't self-closing script elements work?

浏览器无法正确识别的原因是什么:

1
<script src="foobar.js" /> <!-- self-closing script element -->

只有这样才能识别:

1
<script src="foobar.js">

这是否打破了XHTML支持的概念?

注意:此声明至少对所有IE(6-8测试版2)都是正确的。


XHTML 1规格说明:

3。元素最小化和空元素含量

Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use

and not

).

xhtml dtd将脚本元素指定为:

1
2
<!-- script statements, which may include CDATA sections -->
<!ELEMENT script (#PCDATA)>

其他人回答了"如何"并引用了规范。这里是"为什么没有


自关闭脚本标记将不起作用,因为脚本标记可以包含内联代码,并且HTML不够智能,无法基于属性的存在打开或关闭该功能。

On the other hand, HTML does have an excellent tag for including
references to outside resources: the tag, and it can be
self-closing. It's already used to include stylesheets, RSS and Atom
feeds, canonical URIs, and all sorts of other goodies. Why not
JavaScript?

如果您希望脚本标记是自封闭的,那么您不能像我所说的那样做,但是还有一个选择,尽管不是一个聪明的选择。您可以使用自动关闭的链接标记,并通过将文本/javascript和rel类型作为脚本链接到您的javascript,如下所示:

1
<link type="text/javascript" rel ="script" href="/path/tp/javascript" />

与XML和XHTML不同,HTML不了解自动关闭语法。将XHTML解释为HTML的浏览器不知道/字符表示标记应该是自动关闭的;相反,它们将其解释为空属性,解析器仍然认为标记是"打开的"。

正如


脚本元素识别浏览器

最新内容

相关内容

猜你喜欢