此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

Document:documentElement 屬性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Document 介面的 documentElement 唯讀屬性會回傳作為文件根元素的元素(例如,HTML 文件中的 <html> 元素)。

一個 Element 物件。

範例

js
const rootElement = document.documentElement;
const firstTier = rootElement.childNodes;
// firstTier 是根元素的直接子節點的 NodeList
// 像是 <head> 和 <body>

for (const child of firstTier) {
  // 對根元素的每個直接子節點執行某些操作
}

備註

對於任何非空的 HTML 文件,documentElement 總是 <html> 元素。對於任何非空的 XML 文件,documentElement 一定會是該文件的根元素(無論是哪一個元素)。

規範

Specification
DOM
# ref-for-dom-document-documentelement①

瀏覽器相容性