Document.append() メソッド
Document.append()
Document.append()メソッドは、引数の Node オブジェクトまたは DOMString を Document オブジェクトに子オブジェクトとして追加するメソッドです。
しかし仕様どおりではなく、DOMString も引数として使えるとなっていますがエラーになります。
さらに、Document オブジェクトは1つの子エレメントのみ(通常は<html>エレメント)しか受け付けません。 Document が既に子エレメントをもつ場合には追加できません。
var element_ = document.append( node );
詳しくは Example を参照してください
append() メソッドには、Element オブジェクトの Element.append() メソッドがあります。こちらのメソッドは DOMString を受け付け、複数の子オブジェクトを許容します。
Document.append() メソッドは空の Document の場合のみ使うことができます。
似たメソッドに Node.appendChild() があります。このメソッドの引数は Node オブジェクトだけです。append() メソッドは オブジェクト、テキストどちらも受け付けます(ただし、Document オブジェクトではテキストは受け付けない)。
innerHTML プロパティに DOMString でエレメントを追加することもできますが、その動作は保証されたものではありません。
Document.write() メソッドで内容を更新することができますが <body> 内に限られ、その内容の全てが更新されます。
ページ内 Index
- ad -
Syntax
返り値はないです。
Document.append( Node | DOMString );
引数(Parameter Values)
Value | 摘要 |
---|---|
Node | Node オブジェクト。多くの場合は<html>エレメント |
DOMString | テキストノード。Document オブジェクトでは受け付けない |
返り値(Return Values)
返り値はないです。
エラー(Error)
例外エラーについてです。例外エラーは DOMException オブジェクトで返します。
よくあるエラーには以下があります。
Type | 摘要 |
---|---|
Only one element on document allowed | document にはひとつのエレメントオブジェクトのみを受け付けます |
Nodes of type '#text' may not be inserted inside nodes of type '#document' | document ノードにテキストノードは受け付けません |
Example
Document.append() メソッドの例です。新たな Document オブジェクトを作成し、<p> エレメントをそのオブジェクトに追加します。
表示はされませんので、開発用コンソールで確認してください。
- ad -