Movatterモバイル変換


[0]ホーム

URL:


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

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

View in EnglishAlways switch to English

Document: 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月.

Document.prepend() メソッドは、一連のNode オブジェクトまたは文字列オブジェクトを文書の最初の子の前に挿入します。文字列オブジェクトは等価なText ノードとして挿入されます。

このメソッドは子要素をDocument に前置します。ツリー内の任意の要素に前置するにはElement.prepend() を参照してください。

構文

js
prepend(param1)prepend(param1, param2)prepend(param1, param2, /* …, */ paramN)

引数

param1, …,paramN

挿入する一連のNode または文字列オブジェクトです。

返値

なし (undefined)。

例外

HierarchyRequestErrorDOMException

ノードが階層内の指定した点に挿入できなかった場合に発生します。

文書にルート要素を前置

既存の HTML 文書に要素を追加しようとすると、<html> 要素が既に存在する場合は `HierarchyRequestError'DOMException が発生するかもしれません。

js
let html = document.createElement("html");document.prepend(html);// HierarchyRequestError: The operation would yield an incorrect node tree.

既存の要素のない新しい文書を作成する場合、ルート HTML 要素(またはルート SVG 要素)を前置することができます。

js
let doc = new Document();let html = document.createElement("html");doc.prepend(html);doc.children; // HTMLCollection [<html>]

仕様書

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