简体   繁体   English

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

[英]Why some HTML5 tags must have a starting and ending tag instead of self closing them with />?

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中,这种差异不存在: <node/><node></node>是同一个实体。

Why tags like script , div and content can't be defined simply as: <script/> , <div/> and <content/> ? 为什么像scriptdivcontent这样的标签不能简单地定义为: <script/><div/><content/>

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. 在HTML中,此语法的使用仅限于void元素和外部元素。 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. 在XHTML中,任何元素都可以使用此语法。 But note that it is only conforming for elements with content models that permit them to be empty. 但请注意,它只符合内容模型允许它们为空的元素。

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或其他元素,因此具有正确的开始和结束标记将限定这些元素/标记的范围。

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,这个元素可以在没有带尾随/>的结束标记的情况下关闭,但这不是必需的。

These (void elements) may be terminated with /> 这些(空元素)可以用/>终止

area, base, 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 此字符对void元素没有影响

MathML/SVG tags may terminate with /> to indicate a self closing tag. MathML / SVG标记可以使用/>终止以指示自闭标记。

( HTML5 Ref ) HTML5参考

In the HTML5 specifications, some tags are declared as "self closing", and some tags are not. 在HTML5规范中,一些标签被声明为“自闭”,而一些标签则没有。

The script tag is special for that. script标记是特殊的。 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. 当浏览器请求javascript文件(在标记的src属性中给出)时,它将复制script标记中的javascript文件内容。 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将不会被执行。

Since a few years now, HTML (and especially with HTML5) try to get away from XML "borders". 几年以来,HTML(尤其是HTML5)试图摆脱XML“边界”。 That's why we have attributes boolean values (like disabled or checked ). 这就是为什么我们有属性布尔值(如disabledchecked )。

<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. 这是标签实际用于什么的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM