Get the HTML content of a document:
const myBody = document.body.innerHTML;
Try it Yourself »
Change the background color of a document:
document.body.style.backgroundColor = "yellow";
Try it Yourself »
Change the <body> of a document (overwrite all existing content):
document.body.innerHTML = "Some new HTML content";
Try it Yourself »
More examples below.
The body
property sets or returns a document's <body> element.
Setting the body
property overwrites all elements in the document's <body>.
The difference between document.body and document.documentElement:
document.body returns the <body> element.
document.documentElement returns the <html> element.
Return the body property:
document.body
Set the body property:
document.body =
newContent
Value | Description |
newContent | The new content for the <body> element. |
Type | Description |
Object | The body element of the document. |
Create a <p> element and append it to the document's body:
const para = document.createElement("p");
const node = document.createTextNode("This is a paragraph.");
para.appendChild(node);
document.body.appendChild(para);
Try it Yourself »
document.body
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!