このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
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() を参照してください。
In this article
構文
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①> |