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

View in English Always switch to English

Range: createContextualFragment() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Range.createContextualFragment() メソッドは、範囲の先頭ノード(選択されたノードの親)をコンテキストノードとして、 HTML フラグメント構文解析アルゴリズムまたは XML フラグメント構文解析アルゴリズムを呼び出して DocumentFragment を返します。 HTML フラグメント解釈アルゴリズムは、範囲が HTMLness ビットが設定された Document に属している場合に使用します。 HTML では、コンテキストノードが html である場合、歴史的な理由から、フラグメント解釈アルゴリズムは body をコンテキストとして呼び出されます。

構文

js
createContextualFragment(tagString)

引数

tagString

文書フラグメントに変換するテキストとタグを格納するテキストです。

返値

DocumentFragment オブジェクトです。

js
const tagString = "<div>I am a div node</div>";
const range = document.createRange();

// 文書内の最初の div の親をコンテキストノードにします。
range.selectNode(document.getElementsByTagName("div").item(0));
const documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);

仕様書

Specification
HTML
# dom-range-createcontextualfragment

ブラウザーの互換性

関連情報