Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. Web API
  3. Element
  4. prepend()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

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 ノードとして挿入されます。

構文

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①

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp