为什么我们需要< html>标签,如果我们有<!DOCTYPE html&gt ;? [英] Why do we need a <html> tag if we have <!DOCTYPE html>?

查看:89
本文介绍了为什么我们需要< html>标签,如果我们有<!DOCTYPE html&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们已经使用<!DOCTYPE html> 标记定义了此文档为html,为什么还要添加< html> 标记?

Why do we need to add the <html> tag if we have already defined that this document is html using <!DOCTYPE html> tag?

是否可以添加其他标签代替?

Is there any other tag that can be added instead of ?

如果没有,我们为什么需要它?

If not, why do we need it?

推荐答案

在语义级别上...

<!DOCTYPE html> < html> ,尽管它们都包含<尖括号> 是根本不同的东西:

On the semantic level...

<!DOCTYPE html> and <html>, despite both containing < angle brackets >, are fundamentally different things:

  • <!DOCTYPE html> 声明文档类型,并且解析为任何DOM元素.引用 MDN的Doctype文章,其唯一目的是防止浏览器从呈现文档时切换为所谓的怪异模式" "./li>
  • < html> ,但是,执行解析为DOM元素.您可以将属性附加到该属性或使用JavaScript对其进行操作.例如,尝试将其粘贴到浏览器控制台中: document.querySelector('html').style.opacity ='0.5'.您可以使用< html> 进行此操作,但不能使用 DOCTYPE 进行此操作.
  • <!DOCTYPE html> declares the document type and is not parsed to any DOM elements. To quote MDN's Doctype article, "Its sole purpose is to prevent a browser from switching into so-called "quirks mode" when rendering a document".
  • <html>, however, does parse to a DOM element. You can attach attributes to it or manipulate it with JavaScript. For example, try pasting this in your browser console: document.querySelector('html').style.opacity = '0.5'. You can do that with <html>, but not with DOCTYPE.

为什么有任何标准?标准是必不可少的,以便实施者(例如浏览器供应商)知道他们需要构建什么,而开发者也知道他们要针对什么进行开发.没有通用标准,就不可能构建可互操作的软件.

Why have any standards at all? Standards are necessary so that implementers (e.g. browser vendors) know what they need to build, and developers know what they're developing against. Without common standards, it'd be impossible to ever build interoperable software.

您肯定可以质疑为什么为什么标准的设置方式,其答案几乎最终是因为历史"(有时是因为政治").如果这些历史决定现在改变了,那将破坏网络.

You can certainly question the logic of why standards were set the way they were set, and the answer almost ends up at "because history" (and sometimes "because politics"). If those historical decisions were changed now, it'd break the Web.

最佳做法是始终设置 lang HTML元素上的属性,出于可访问性和SEO的原因,您必须将其包括在内才能实现:

It's best practice to always set a lang attribute on the HTML element, for accessibility and SEO reasons, and you need to include it in order to do that:

始终在html标记上使用language属性,以声明页面中文本的默认语言.

Always use a language attribute on the html tag to declare the default language of the text in the page.

在编写代码或标记以明确说明您的意图时,这也是一般的最佳做法,以帮助其他人阅读您的作品.仅出于这个原因,添加周围的HTML标签就更加清楚了.

It's also a general best practice when writing code or markup to be explicit with what you intend, to help others reading your work. For that reason alone, it's clearer to add a surrounding HTML tag.

如果您在HTML元素上包括任何属性,那么就解析方式而言,您写的内容实际上并不重要,因为

If you don't include any attributes on the HTML element, it doesn't actually matter what you write in terms of how it's parsed, because HTML is quite forgiving) (credit @Run_Script for the link). It'll get parsed out to the relevant DOM nodes anyway, so document.body.parentElement instanceof HTMLHtmlElement will always be true.

不过,出于上述原因,我建议始终将其包括在内.

Still, for the reasons laid out above, I'd recommend always including it.

这篇关于为什么我们需要&lt; html&gt;标签,如果我们有&lt;!DOCTYPE html&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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