例如,下面这个最常见的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).
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决