DOM

Document.append() メソッド

 編集:2022.09.07 by T.A.

Document の最後の子の後に、指定のNode オブジェクトまたは DOMString オブジェクトを挿入します。

String オブジェクトは、同等の Text ノードとして挿入されます。

このメソッドは子を Document に追加します。ツリー内の任意の要素に追加するには、Element.append() を参照してください。

Document.append(param1[, param2[, [, paramN]]])
Document Document
paramn 挿入するノードまたは文字列オブジェクトのセット。¶文字列はできない?
戻り値

なし(undefined)。

例外
HierarchyRequestError(DOMException
階層内の指定されたポイントにノードを挿入できない場合にスローされます。
ドキュメントへのルート要素の追加

既存の HTML ドキュメントに要素を追加しようとすると、<html> 要素が既に存在する場合、HierarchyRequestError(DOMException)をスローする可能性があります。

let html = document.createElement("html");
document.append(html);
// HierarchyRequestError: この操作により、不適切なノード ツリーが生成されます。

既存の要素なしで新しいドキュメントを作成する場合は、ルート HTML 要素(またはルート SVG 要素)を追加できます。

//コンソールにて
let doc = new Document();
let html = document.createElement("html");
doc.append(html);
doc.children; // HTMLCollection []
doc=new Document();	//Document オブジェクトの生成。
html=document.createElement("html");	//<HTML> 要素作成。
doc.append(html);	//<HTML> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
D=doc.children; //=[object]:[object HTMLCollection]。Documentオブジェクトの子要素のリストを得る。
	//0=[object]:[object HTMLHtmlElement]
	//length=[number]:1
	//item=[function]:function item() { [native code] }
	//namedItem=[function]:function namedItem() { [native code] }
doc=new Document();	//Document オブジェクトの生成。
doc.append("てすと");	//文字列を生成したDocumentオブジェクトに組み込む。
	//エラー発生:Failed to execute 'append' on 'Document': Nodes of type '#text' may not be inserted inside nodes of type '#document'.
	//'Document' で 'append' を実行できませんでした: タイプ '#text' のノードは、タイプ '#document' のノード内に挿入できない場合があります。
doc=new Document();	//Document オブジェクトの生成。
html=document.createElement("html");	//<HTML> 要素作成。
doc.append(html);	//<HTML> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
doc.append("てすと");	//文字列を生成したDocumentオブジェクトに組み込む。
	//エラー発生:Failed to execute 'append' on 'Document': Nodes of type '#text' may not be inserted inside nodes of type '#document'.
doc=new Document();	//Document オブジェクトの生成。
html=document.createElement("html");	//<HTML> 要素作成。
doc.append(html);	//<HTML> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
Body=document.createElement("body");	//<BODY> 要素作成。
doc.append(Body);	//<BODY> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
	//エラー発生:Failed to execute 'append' on 'Document': Only one element on document allowed.
	//'Document' で 'append' を実行できませんでした: ドキュメントで許可されている要素は 1 つだけです。
doc=new Document();	//Document オブジェクトの生成。
html=document.createElement("html");	//<HTML> 要素作成。
doc.append(html);	//<HTML> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
S=String("てすと");
doc.append(S);	//<BODY> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
	//エラー発生:Failed to execute 'append' on 'Document': Nodes of type '#text' may not be inserted inside nodes of type '#document'.
doc=new Document();	//Document オブジェクトの生成。
S=String("てすと");
doc.append(S);	//<BODY> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
	//エラー発生:Failed to execute 'append' on 'Document': Nodes of type '#text' may not be inserted inside nodes of type '#document'.

¶文字列オブジェクトは appen() できない!

doc=new Document();	//Document オブジェクトの生成。
html=document.createElement("html");	//<HTML> 要素作成。
doc.append(html);	//<HTML> 要素オブジェクトを生成したDocumentオブジェクトに組み込む。
Body=document.createElement("body");	//<BODY> 要素作成。
html.append(Body);	//<BODY> 要素オブジェクトを生成したhtml要素オブジェクトに組み込む(Element.append()使用)。

テスト