このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
Element: prepend() メソッド
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月.
Element.prepend() メソッドは、一連のNode または文字列をこのElement の最初の子の前に挿入します。文字列は、同等のText ノードとして挿入されます。
In this article
構文
js
prepend(param1)prepend(param1, param2)prepend(param1, param2, /* …, */ paramN)引数
param1, …,paramN挿入する一連の
Nodeまたは文字列です。
返値
なし (undefined)。
例外
HierarchyRequestErrorDOMExceptionノードを階層の指定された箇所に追加することができない場合に発生します。
例
>要素の前に追加
js
let div = document.createElement("div");let p = document.createElement("p");let span = document.createElement("span");div.append(p);div.prepend(span);console.log(div.childNodes); // NodeList [ <span>, <p> ]テキストの前に追加
js
let div = document.createElement("div");div.append("Some text");div.prepend("Headline: ");console.log(div.textContent); // "Headline: Some text"要素とテキストの追加
js
let div = document.createElement("div");let p = document.createElement("p");div.prepend("Some text", p);console.log(div.childNodes); // NodeList [ #text "Some text", <p> ]prepend() メソッドはスコープが効かない
prepend() メソッドはwith 文の中ではスコープが効きません。詳しくはSymbol.unscopables をご覧ください。
js
let div = document.createElement("div");with (div) { prepend("foo");}// ReferenceError: prepend is not defined仕様書
| Specification |
|---|
| DOM> # ref-for-dom-parentnode-prepend①> |