Document: append() メソッド
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年4月.
Document.append() メソッドは、一連の Node オブジェクトまたは文字列オブジェクトを、この文書の最後の子の後に挿入します。文字列オブジェクトは等価な Text ノードとして挿入されます。
このメソッドは Document に子要素を追加します。ツリー内の任意の要素に追加するには Element.append() を参照してください。
構文
js
append(param1)
append(param1, param2)
append(param1, param2, /* …, */ paramN)
引数
param1, …,paramN-
挿入する一連の
Nodeオブジェクトまたは文字列オブジェクトです。
返値
なし (undefined)。
例外
HierarchyRequestErrorDOMException-
ノードが階層内の指定した点に挿入できない場合に発生します。
例
>ルート要素を文書に追加
既存の HTML 文書に要素を追加しようとすると、<html> 要素が既に存在する場合は HierarchyRequestError DOMException が発生するかもしれません。
js
let html = document.createElement("html");
document.append(html);
// HierarchyRequestError: The operation would yield an incorrect node tree.
既存の要素のない新しい文書を作成した場合、ルート HTML 要素(またはルート SVG 要素)を追加することができます。
js
let doc = new Document();
let html = document.createElement("html");
doc.append(html);
doc.children; // HTMLCollection [<html>]
仕様書
| Specification |
|---|
| DOM> # ref-for-dom-parentnode-append①> |
ブラウザーの互換性
Loading…