为什么有些HTML5标签必须有一个开始和结束标签,而不是用/>关闭它们?

时间:2022-11-14 08:49:34

In HTML5 there are tags that must have a starting and an ending tag even when their content is empty:

在HTML5中,标签必须有一个开始和结束标签,即使他们的内容是空的:

<script></script>   <!-- Right way. -->
<div></div>         <!-- Right way. -->
<content></content> <!-- Right way. -->

<script/>  <!-- Wrong way. -->
<div/>     <!-- Wrong way. -->
<content/> <!-- Wrong way. -->

In XML this difference doesn't exist: <node/> and <node></node> are the same entity.

在XML中,这种差异不存在: 是相同的实体。

Why tags like script, div and content can't be defined simply as: <script/>, <div/> and <content/>?

为什么像script、div和content这样的标签不能简单地定义为:,

?

5 个解决方案

#1


4  

From the w3c:

w3c:

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag. In XHTML, it is possible for any element to use this syntax. But note that it is only conforming for elements with content models that permit them to be empty.

自闭标记是开始标记的一种特殊形式,在右括号前加上斜线。这表明元素将立即关闭,并且没有内容。如果允许并使用此语法,则必须省略结束标记。在HTML中,这种语法的使用仅限于空元素和外元素。如果将其用于其他元素,则将其视为开始标记。在XHTML中,任何元素都可以使用这种语法。但是请注意,它只符合允许它们为空的内容模型的元素。

The examples you listed would generally contain content, JavaScript, or other elements, so having a proper start and end tag would delimit the scope of those elements/tags.

您列出的示例通常包含内容、JavaScript或其他元素,因此拥有一个合适的开始和结束标记将分隔这些元素/标记的范围。

#2


2  

Simply because they are basically some "container" for other elements.

因为它们基本上是其他元素的“容器”。

There are elements which are didn't used as parent elements for others, like img or base for example, this one can be closed without an closing tag with a trailing />, but it is not necessary.

有一些元素没有作为其他元素的父元素,例如img或base,这个元素可以在没有末尾/>的结束标记的情况下关闭,但这是不必要的。

#3


1  

These (void elements) may be terminated with />

这些(无效元素)可以用/>终止

area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

区域,基地,br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

However its optional and irrelevant:

但是,它是可选的和无关的:

This character has no effect on void elements

这个角色对虚空元素没有影响

MathML/SVG tags may terminate with /> to indicate a self closing tag.

MathML/SVG标记可以使用/>终止,以指示一个自关闭标记。

(HTML5 Ref)

HTML5(Ref)

#4


0  

In the HTML5 specifications, some tags are declared as "self closing", and some tags are not.

在HTML5规范中,有些标签被声明为“自闭”,有些标签则不是。

The script tag is special for that. When a browser request a javascript file (given in the src attribute of the tag), it will copy the javascript file content in the script tag. And if you self close the tag, the browser can't copy any content in the tag so the javascript will not be executed.

这个脚本标签是特别的。当浏览器请求一个javascript文件(在标记的src属性中给出)时,它将复制脚本标记中的javascript文件内容。如果你自己关闭标签,浏览器就不能复制标签中的任何内容,这样javascript就不会被执行。

Since a few years now, HTML (and especially with HTML5) try to get away from XML "borders". That's why we have attributes boolean values (like disabled or checked).

几年来,HTML(尤其是HTML5)一直试图摆脱XML的“边界”。这就是为什么我们有属性布尔值(比如禁用或检查)。

#5


-1  

<script></script>   <!-- Right way. -->
<div></div>         <!-- Right way. -->
<content></content> <!-- Right way. -->

In all 3 of those cases, there is an indefinite amount of content that could reside within the tag.

在所有这三种情况中,都有不确定数量的内容可以驻留在标记中。

With something like:

可以这样说:

<input type="text" velue="Hello" />
<img src="image.jpg" />

There is no more content that you could possibly add to these. So you can simply close the tags at the same point that they are generated.

没有更多的内容可以添加到这些。所以你可以在生成标签的同时关闭标签。

So, it's not a matter of "right or wrong". It's a matter of what the tag is actually used for.

所以,这不是“对还是错”的问题。这是一个标签实际用途的问题。

#1


4  

From the w3c:

w3c:

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag. In XHTML, it is possible for any element to use this syntax. But note that it is only conforming for elements with content models that permit them to be empty.

自闭标记是开始标记的一种特殊形式,在右括号前加上斜线。这表明元素将立即关闭,并且没有内容。如果允许并使用此语法,则必须省略结束标记。在HTML中,这种语法的使用仅限于空元素和外元素。如果将其用于其他元素,则将其视为开始标记。在XHTML中,任何元素都可以使用这种语法。但是请注意,它只符合允许它们为空的内容模型的元素。

The examples you listed would generally contain content, JavaScript, or other elements, so having a proper start and end tag would delimit the scope of those elements/tags.

您列出的示例通常包含内容、JavaScript或其他元素,因此拥有一个合适的开始和结束标记将分隔这些元素/标记的范围。

#2


2  

Simply because they are basically some "container" for other elements.

因为它们基本上是其他元素的“容器”。

There are elements which are didn't used as parent elements for others, like img or base for example, this one can be closed without an closing tag with a trailing />, but it is not necessary.

有一些元素没有作为其他元素的父元素,例如img或base,这个元素可以在没有末尾/>的结束标记的情况下关闭,但这是不必要的。

#3


1  

These (void elements) may be terminated with />

这些(无效元素)可以用/>终止

area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

区域,基地,br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

However its optional and irrelevant:

但是,它是可选的和无关的:

This character has no effect on void elements

这个角色对虚空元素没有影响

MathML/SVG tags may terminate with /> to indicate a self closing tag.

MathML/SVG标记可以使用/>终止,以指示一个自关闭标记。

(HTML5 Ref)

HTML5(Ref)

#4


0  

In the HTML5 specifications, some tags are declared as "self closing", and some tags are not.

在HTML5规范中,有些标签被声明为“自闭”,有些标签则不是。

The script tag is special for that. When a browser request a javascript file (given in the src attribute of the tag), it will copy the javascript file content in the script tag. And if you self close the tag, the browser can't copy any content in the tag so the javascript will not be executed.

这个脚本标签是特别的。当浏览器请求一个javascript文件(在标记的src属性中给出)时,它将复制脚本标记中的javascript文件内容。如果你自己关闭标签,浏览器就不能复制标签中的任何内容,这样javascript就不会被执行。

Since a few years now, HTML (and especially with HTML5) try to get away from XML "borders". That's why we have attributes boolean values (like disabled or checked).

几年来,HTML(尤其是HTML5)一直试图摆脱XML的“边界”。这就是为什么我们有属性布尔值(比如禁用或检查)。

#5


-1  

<script></script>   <!-- Right way. -->
<div></div>         <!-- Right way. -->
<content></content> <!-- Right way. -->

In all 3 of those cases, there is an indefinite amount of content that could reside within the tag.

在所有这三种情况中,都有不确定数量的内容可以驻留在标记中。

With something like:

可以这样说:

<input type="text" velue="Hello" />
<img src="image.jpg" />

There is no more content that you could possibly add to these. So you can simply close the tags at the same point that they are generated.

没有更多的内容可以添加到这些。所以你可以在生成标签的同时关闭标签。

So, it's not a matter of "right or wrong". It's a matter of what the tag is actually used for.

所以,这不是“对还是错”的问题。这是一个标签实际用途的问题。