文档:documentElement 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Document 接口的只读属性 documentElement 返回 Document 的根元素(例如,对于 HTML 文档,即 <html> 元素)。

一个 Element 对象。

示例

js
const rootElement = document.documentElement;
const firstTier = rootElement.childNodes;
// firstTier is a NodeList of the direct children of the root element
// such as <head> and <body>

for (const child of firstTier) {
  // do something with each direct child of the root element
}

注意

对于任何非空 HTML 文档,documentElement 始终是 <html> 元素。对于任何非空 XML 文档,documentElement 始终是文档的根元素。

规范

规范
DOM
# ref-for-dom-document-documentelement①

浏览器兼容性