文件: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①

瀏覽器相容性