DOM

Document.documentElement プロパティ

 編集:2022.09.07 by T.A.

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

構文
Document.documentElement

Element

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

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

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

テスト