HTML DOM bodyProperty

Document 对象参考手册Document Object

Examples

Change the background color of the current document:

document.body.style.backgroundColor = "yellow";

Try it Yourself »

Definition and Usage

The body property is used to set or return the document body.

When returning, this property returns the current document's<body>element.

When setting, this property overwrites all child elements in the <body> element and replaces them with new content.

Tip:anddocument.documentElementThe difference between the properties is that the document.body property returns the <body> element, while the document.documentElement property returns the <html> element.


Browser Support

Property
body Yes Yes Yes Yes Yes

Syntax

Return the body property:

document.body

Set the body property:

document.body = newContent

Property

Value Description
newContent Specifies the new content of the <body>

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: A reference to the Body object, representing the <body> element


More Examples

Example

Get the HTML content of the current document:

var x = document.body.innerHTML;

Try it Yourself »

Example

Modify the HTML content of the current document:

document.body.innerHTML = "New content...";

Try it Yourself »

Example

Create a <p> element and add it to the document:

var x = document.createElement("P"); //Create a <p> element var t = document.createTextNode("This is a newly added paragraph."); //Create a text node x.appendChild(t); //Add the text to the <p> element document.body.appendChild(x); //Add the <p> to the <body>

Try it Yourself »

Related Pages

HTML Reference Manual:HTML <body> Tag

JavaScript Reference Manual:HTML DOM Body Object

Document 对象参考手册Document Object

Other Extensions