为什么<!Doctype...>标签没有闭合?

4

这是一个普遍的问题,我的教授们都无法回答: 为什么在HTML中不需要关闭<!Doctype>标签呢? 每个其他的标签都需要关闭,例如<head></head>或者<html></html>! 那么为什么<!Doctype>不需要关闭呢?

3个回答

13
首先,这不是一个标签。在基于SGML或XML标准的HTML版本中,它是一个文档类型声明,具有SGML和XML中定义的特定语法。作为关于整个文档的声明,它与HTML元素完全不同,位于元素之前。在HTML语法中的HTML5版本中,它只是一个必需的字符串,没有定义的角色。(实际上,它将浏览器设置为“标准模式”而非“怪异模式”。浏览器实际上并没有读取声明所指向的文档类型定义。验证器可以这样做。)
相关参考资料包括SGML标准(不可在线获取)、XML规范HTML5 CR
The formulation of the statement “Every other tag has to be closed” in the question is misleading in other ways, too. Closing a tag (like <p>) and closing an element (like <p>foo</p>) are two different things. The > closes a tag. Elements do not always need explicit end tags, since the end can be inferred by various rules; only XHTML requires explicit end tags for all elements (if we count a “self-closing” tag like <br/> as both a start tag and an end tag, as it is). But for this doctype thing, no end needs to be indicated or inferred, except the ending > of the declaration.

很棒的解释,伙计 :) - CvR

1

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接