Document.documentElement

Document.documentElement は、その document のルート要素 (例えば、 HTML 文書の場合は <html> 要素) である Element を返します。

構文

const element = document.documentElement

js
const rootElement = document.documentElement;
const firstTier = rootElement.childNodes;
// firstTier は <head> や <body> などの
// ルート要素の直接の子である NodeList

for (const child of firstTier) {
  // ルート要素のそれぞれの直接の子に対する処理
}

メモ

空ではない HTML 文章の場合、 documentElement は常に <html> 要素を返します。空ではない XML 文章の場合、 documentElement は常に文章のルート要素である何らかの要素を返します。

仕様書

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

ブラウザーの互換性

BCD tables only load in the browser