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> 要素の子ノードは全て削除されます。

仕様書

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.

関連情報