gpt4 book ai didi

html - 为什么有些 HTML5 标签必须有开始和结束标签,而不是用/> 自行结束?

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:11 24 4
gpt4 key购买 nike

在 HTML5 中,有些标签必须有开始和结束标签,即使它们的内容为空也是如此:

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

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

在 XML 中不存在这种差异: <node/><node></node>是同一个实体。

为什么标签像 script , divcontent不能简单定义为: <script/> , <div/><content/>

最佳答案

From the 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.

您列出的示例通常包含内容、JavaScript 或其他元素,因此具有适当的开始和结束标记将界定这些元素/标记的范围。

关于html - 为什么有些 HTML5 标签必须有开始和结束标签,而不是用/> 自行结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33282917/

24 4 0