CharacterData: replaceData() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

replaceData()CharacterData インターフェイスのメソッドで、この CharacterData ノードにある既存のテキストの特定の数の文字を取り除き、指定されたテキストの文字で置き換えます。

構文

js
characterData.replaceData(offset, count, data)

引数

offset

データの先頭から何文字目に挿入するかを指定します。 0 は文字列の先頭の文字を表します。

count

指定されたデータで置換する文字数。

data

挿入するデータです。

返値

なし。

例外

IndexSizeError DOMException

offset または count が負の値であるか、 offset が含まれるデータの長さよりも大きい場合に発生します。

html
<span>Result: </span>A long string.
js
const span = document.querySelector("span");
const textnode = span.nextSibling;

textnode.replaceData(2, 4, "replaced");

仕様書

Specification
DOM
# dom-characterdata-replacedata

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
replaceData

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報