--- title: Document.body slug: Web/API/Document/body translation_of: Web/API/Document/body ---
{{APIRef("DOM")}}

回傳目前文件的 {{HTMLElement("body")}} 或 {{HTMLElement("frameset")}} 節點,如元素不存在則回傳 null

語法

var objRef = document.body;
document.body = objRef;

範例

// in HTML: <body id="oldBodyElement"></body>
alert(document.body.id); // "oldBodyElement"

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

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

備註

document.body is the element that contains the content for the document. In documents with <body> contents, returns the <body> element, and in frameset documents, this returns the outermost <frameset> element.

Though body is settable, setting a new body on a document will effectively remove all the current children of the existing <body> element.

規範

Specification Status Comment
{{SpecName('HTML WHATWG','dom.html#dom-document-body','Document.body')}} {{Spec2('HTML WHATWG')}}  
{{SpecName('HTML5.1','dom.html#dom-document-body','Document.body')}} {{Spec2('HTML5.1')}}  
{{SpecName('HTML5 W3C','dom.html#dom-document-body','Document.body')}} {{Spec2('HTML5 W3C')}}  
{{SpecName('DOM2 HTML','html.html#ID-56360201','Document.body')}} {{Spec2('DOM2 HTML')}}  
{{SpecName('DOM1','level-one-html.html#attribute-body','Document.body')}} {{Spec2('DOM1')}} Initial definition.

瀏覽器相容性

{{CompatibilityTable}}
Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1 {{CompatVersionUnknown}} 2 6 9.6 (possibly earlier) 4 (possibly earlier)
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} 5 (probably earlier)

參見