Document.head

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.

回傳當前文件的 <head> 元素。如果該文件有超過一個 <head> 元素,那只會回傳第一個 <head> 元素。

語法

var objRef = document.head;

範例

js
// in HTML: <head id="my-document-head">
var aHead = document.head;

alert(aHead.id); // "my-document-head";

alert(document.head === document.querySelector("head")); // true

備註

document.head 是「唯讀」。若是想要將 document.head 改成別的值會失敗,這時有些瀏覽器不會告知任何錯誤訊息;有些,例如在 ECMAScript Strict 模式下 的 Gecko 瀏覽器,會給出 TypeError 異常。

規範

Specification
HTML
# dom-document-head-dev

瀏覽器相容性

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
head

Legend

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

Full support
Full support
Partial support
Partial support
Has more compatibility info.

參見