Document.body

La proprietà Document.body rappresenta il nodo <body><frameset> del documento corrente, o null se non esiste alcun elemento di questo tipo.

Sintassi

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

Esempio

// Dato questo 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"

Appunti

document.body è l'elemento che contiene il contenuto per il documento. Nei documenti con contenuto <body> restituisce l'elemento <body>, e nei documenti frameset, restituisce l'elemento <frameset> più esterno.

Anche se body è settabile, l'impostazione di un nuovo corpo su un documento rimuoverà efficacemente tutti i figli attuali dell'elemento <body>.

Specifiche

Specifica Stato Commento
HTML Living Standard
The definition of 'Document.body' in that specification.
Living Standard  
HTML 5.1
The definition of 'Document.body' in that specification.
Recommendation  
HTML5
The definition of 'Document.body' in that specification.
Recommendation  
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'Document.body' in that specification.
Obsolete  
Document Object Model (DOM) Level 1 Specification
The definition of 'Document.body' in that specification.
Obsolete Definizione iniziale.

Compatibilità con i browser

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
bodyChrome Full support 1Edge Full support 12Firefox Full support 2
Notes
Full support 2
Notes
Notes The body property was implemented on the HTMLDocument interface in Firefox for a long time, hence document.body would not return the <body> element if the document's Content-Type was not set to text/html or application/xhtml+xml (or if it came from DOMParser.parseFromString without the text/html type being used). This has been fixed in Firefox 60.
IE Full support 6Opera Full support 9.6Safari Full support 4WebView Android Full support YesChrome Android Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes The body property was implemented on the HTMLDocument interface in Firefox for a long time, hence document.body would not return the <body> element if the document's Content-Type was not set to text/html or application/xhtml+xml (or if it came from DOMParser.parseFromString without the text/html type being used). This has been fixed in Firefox 60.
Opera Android Full support YesSafari iOS Full support 5Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

Vedi anche