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.

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
# ref-for-dom-document-documentelement①

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
documentElement

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support