简体   繁体   English

为什么 DOCTYPE 声明不是 HTML 元素或 HTML 标签?

[英]Why is DOCTYPE declaration not a HTML element or HTML tag?

Why and what is the reason behind for the DOCTYPE declaration to not be a valid HTML element or tag? DOCTYPE 声明不是有效的 HTML 元素或标签的原因是什么? Would it be considered as valid HTML code though?它会被认为是有效的 HTML 代码吗?

I do know that HTML5 standard defines tags as below:我知道 HTML5 标准定义的标签如下:

Tags are used to delimit the start and end of elements in the markup.标记用于分隔标记中元素的开始和结束。

However, there are self closing tags such that do not delimit any text content as an element either, which led me to be a bit confused and ask here.但是,有一些自闭标签,它们也不会将任何文本内容定界为元素,这让我有点困惑并在这里提问。

Technically <!DOCTYPE > is not a tag/element, it just an instruction to the browser about the document type or to provide links to a set of rules that the HTML page had to follow, which could mean automatic error checking etc.从技术上讲, <!DOCTYPE >不是标签/元素,它只是向浏览器发出有关文档类型的指令,或提供指向 HTML 页面必须遵循的一组规则的链接,这可能意味着自动错误检查等。

Because it doesn't need to be a tag.因为它不需要是标签。 It's almost a redundancy for the .html file extension.它几乎是.html文件扩展名的冗余。 It just tells the browser the type of document that it is receiving.它只是告诉浏览器它正在接收的文档类型。

Would it be considered as valid HTML code though?它会被认为是有效的 HTML 代码吗?

To make your code valid, it is required (not all of the time in preset environments), if that is your question.为了使您的代码有效,如果这是您的问题,则需要(在预设环境中并非所有时间)。

However, there are self-closing tags that do not delimit any text content as an element either, which led me to be a bit confused and ask here.但是,有些自闭合标签也不将任何文本内容作为元素来分隔,这让我有点困惑并在这里问。

These are elements that carry information in their attributes (such as input, img, etc).这些是在其属性(例如 input、img 等)中携带信息的元素。 The DOCTYPE declaration has no attributes (aside from the type of HTML) and thus, does not need to be an HTML tag or element. DOCTYPE声明没有属性(除了 HTML 的类型),因此不需要是 HTML 标记或元素。

There are some other reasons that go into DOM parsing which you can read about in this question if you're interested. go 进入 DOM 解析还有其他一些原因,如果您有兴趣,可以在这个问题中阅读。

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

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