Document:body 属性

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.body 属性表示当前文档的 <body><frameset> 节点,如果不存在此类元素,则为 null

以下其中一项:

示例

js
// 给定以下 HTML:<body id="oldBodyElement"></body>
alert(document.body.id); // “oldBodyElement”

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

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

备注

document.body 是包含文档内容的元素。在具有 <body> 内容的文档中,返回 <body> 元素,在框架集文档中,返回最外层的 <frameset> 元素。

虽然 body 属性是可设置的,但在文档中设置一个新的 body 会有效地移除现有 <body> 元素的所有当前子元素。

规范

Specification
HTML
# dom-document-body-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
body

Legend

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

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

参见