DOM

Document.body プロパティ

 編集:2022.08.29 by T.A.

その Document の <body> または <frameset> ノードを返します。そのような要素がなければ null になります。

構文
Document.body
Document Document
注意

document.body は文書の内容を持つ要素です。<body> の内容を持つ文書では <body> 要素を返し、フレームセット文書では、これは最も外側の <frameset> 要素を返します。

body プロパティは設定が可能ですが、新しい内容を設定すると、既存の <body> 要素の子ノードは全て削除されます。

// Given this 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"
Doc=new Document();	//新しい Document オブジェクトを作成
D=Doc.body;	// その Document の Body オブジェクトを取得。
	//=[object]:null
D=Doc.children;	//Document の子要素コレクションを取得。
	//=[object]:[object HTMLCollection]
	//length=[number]:0
	//item=[function]:function item() { [native code] }
	//namedItem=[function]:function namedItem() { [native code] }

↑Documentには要素が無い状態のようだ。

D=document.body;	//当文書の Document の Body オブジェクトを取得。
	//=[object]:[object HTMLBodyElement]
	//text=[string]:
	//link=[string]:
	//vLink=[string]:
	//aLink=[string]:
	//bgColor=[string]:
	//background=[string]:
	//…

要素が無い状態のようだ。


テスト