Document.documentElement

Document.documentElement 會回傳目前文件(document (en-US))中的根元素(Element (en-US)),如: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> (en-US) 元素 ; 對於所有非空的 XML 文件,document.documentElement 則會是文件的根元素。

規範

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

瀏覽器相容性

BCD tables only load in the browser