HTML DOM insertAdjacentHTML()メソッド
例
ヘッダー要素の後に新しい<p>要素を挿入します。
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
定義と使用法
このinsertAdjacentHTML()
メソッドは、テキストをHTMLとして指定された位置に挿入します。
法定位置の値は次のとおりです。
「afterbegin」
「afterend」
「beforebegin」
「beforeend」
ブラウザのサポート
表の数字は、このメソッドを完全にサポートする最初のブラウザバージョンを示しています。
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
構文
node.insertAdjacentHTML(position, text)
パラメータ値
Parameter | Type | Description |
---|---|---|
position | String | Required. A position relative to the element. Legal values: "afterbegin" - After the beginning of the element (as the first child) "afterend" - After the element "beforebegin" - Before the element "beforeend" - Before the end of the element (as the last child) |
text | String | The text you want to insert |
その他の例
例
「afterbegin」値の使用:
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
例
「beforebegin」値の使用:
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
例
「beforeend」値の使用:
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");