3 个回答
document.querySelector('html') === document.documentElement

例如,下面这个最常见的html文件。
foo.html

<!DOCTYPE html>
<html>
<head>
    <title>文档的标题</title>
</head>
<body>
    文档的内容......
</body>

</html>

此时的document就指的是这个foo.html文档,而document.documentElement则是<html>。
document表示整个文档

Object.prototype.toString.call(document)
//return "[object HTMLDocument]"

document.documentElement表示取得对<html>的引用

Object.prototype.toString.call(document.documentElement)
//return "[object HTMLHtmlElement]

你还可以取得body,doctype,title,URL,domain等的引用,例如
document.body
document.doctype
...
注意doctype,可以帮助你理解二者的不同。

https://developer.mozilla.org...

Document.documentElement returns the Element that is the root element of the document (for example, the <html> element for HTML documents).

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题