值
以下之一:
示例
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 |
瀏覽器相容性
載入中…