DOMTokenList.forEach()

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

構文

forEach(callback);
forEach(callback, thisArg);

引数

callback

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

currentValue

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

currentIndex

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

listObj

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

thisArg 省略可

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

返値

なし。

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

HTML

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

JavaScript

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.

ブラウザーの互換性

BCD tables only load in the browser

関連情報