文件:body 屬性

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上執行。自 2018 年 5 月起,它已在各個瀏覽器中可用。

Document.body 屬性表示當前文件的 <body><frameset> 節點,如果不存在這樣的元素則為 null

以下之一:

示例

js
// Given this HTML: <body id="oldBodyElement"></body>
alert(document.body.id); // "oldBodyElement"

const newBodyElement = document.createElement("body");

newBodyElement.id = "newBodyElement";
document.body = newBodyElement;
alert(document.body.id); // "newBodyElement"

注意

document.body 是包含文件內容的元素。在包含 <body> 內容的文件中,它返回 <body> 元素;在 frameset 文件中,它返回最外層的 <frameset> 元素。

雖然 body 屬性是可設定的,但在文件上設定新的 body 將有效地移除現有 <body> 元素的所有當前子元素。

規範

規範
HTML
# dom-document-body-dev

瀏覽器相容性

另見