DOMTokenList: forEach() メソッド

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.

forEach()DOMTokenList インターフェイスのメソッドで、リスト中のそれぞれの値の組に対して挿入順で 1 回ずつ、引数で渡されたコールバックを呼び出します。

構文

js
forEach(callback)
forEach(callback, thisArg)

引数

callback

それぞれの要素に対して呼び出す関数で、 3 つの引数を取ります。

currentValue

配列内で処理中の現在の要素です。

currentIndex

配列内で処理中の現在の要素の位置です。

listObj

forEach() を実行中の配列です。

thisArg 省略可

callback を実行する際に this として使用する値です。

返値

なし。

次の例では、<pre> 要素に設定されたクラスのリストを DOMTokenList として受け取るのに Element.classList を使用しています。forEach() を使用して値を含むイテレーターを取得し、それぞれの値を <pre>Node.textContentforEach() の中の関数から書き込みます。

HTML

html
<pre class="a b c"></pre>

JavaScript

js
const pre = document.querySelector("pre");
const classes = pre.classList;
const iterator = classes.values();

classes.forEach(function (value, key, listObj) {
  pre.textContent += `(${value} ${key})/${this}\n`;
}, "arg");

結果

仕様書

No specification found

No specification data found for api.DOMTokenList.forEach.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

ブラウザーの互換性

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
forEach

Legend

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

Full support
Full support

関連情報