为什么<!Doctype ...>-标记未关闭 [英] Why is the <!Doctype ... >-Tag not closed

查看:151
本文介绍了为什么<!Doctype ...>-标记未关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个普遍的问题,我的教授们都无法回答:为什么我不必关闭HTML中的<!Doctype> -Tag?其他所有标签都必须关闭,例如< head></head> < html></html> !那么为什么不<!Doctype> ?

It's a general quuestion, and none of my profs could answer it: Why do I not have to close the <!Doctype >-Tag in HTML? Every other tag has to be closed, like <head></head> or <html></html>! So why not <!Doctype>?

推荐答案

首先,它根本不是标签.在名义上基于SGML或XML的HTML版本中,它是一个文档类型声明,它具有在SGML和XML中定义的特定语法.作为整个文档的声明,它与HTML元素完全不同,它位于元素之前.在采用HTML语法的HTML5中,它只是必需的字符串,没有定义角色.(实际上,它使浏览器进入标准模式",而不是怪异模式".浏览器实际上并不读取声明指向的文档类型定义.验证者可以这样做.)

First of all, it’s not a tag at all. In HTML versions nominally based on SGML or XML, it’s a document type declaration, which has a specific syntax defined in SGML and XML. Being a declaration about the document as a whole, it’s nothing like HTML elements, and it precedes the elements. In HTML5 in HTML syntax, it’s just a required string with no defined role. (In practice, it puts browsers to "standards mode" as opposite to "quirks mode". Browsers do not actually read the document type definition that the declaration points to. Validators may do that.)

相关参考资料是SGML标准(在线不可用), XML规范 HTML5 CR .

The relevant references are the SGML standard (not available online), the XML specification, and the HTML5 CR.

问题中其他所有标签都必须关闭"的表述在其他方面也具有误导性.关闭标签(如< p> )和关闭元素(如&p; foo</p> )是两件不同的事情.> 关闭标签.元素并不总是需要显式的结束标记,因为可以通过各种规则来推断结束.只有XHTML要求所有元素都使用显式的结束标签(如果我们将"<自闭"标签,例如< br/> 当作开始标签和结束标签,则按原样).但是对于此doctype而言,除了声明的结尾> 外,不需要指示或推断出结尾.

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.

这篇关于为什么&lt;!Doctype ...&gt;-标记未关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆