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 July 2015.

io error: No such file or directory (os error 2) (/home/runner/work/yari/yari/mdn/translated-content/files/zh-tw/web/api/document/index.md)

Document.documentElement 會回傳目前文件(document)中的根元素(Element),如:HTML 文件中的 <html> 元素。

語法

var element = document.documentElement;

範例

js
var rootElement = document.documentElement;
var firstTier = rootElement.childNodes;

// firstTier is the NodeList of the direct children of the root element
for (var i = 0; i < firstTier.length; i++) {
  // do something with each direct kid of the root element
  // as firstTier[i]
}

備註

對於所有非空的 HTML 文件, document.documentElement 將會是一個 <html> 元素 ; 對於所有非空的 XML 文件,document.documentElement 則會是文件的根元素。

規範

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

瀏覽器相容性

BCD tables only load in the browser