Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. DOMTokenList
  4. forEach()

DOMTokenList: forEach() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨October 2017⁩.

TheforEach() method of theDOMTokenList interfacecalls the callback given in parameter once for each value pair in the list, ininsertion order.

Syntax

js
forEach(callback)forEach(callback, thisArg)

Parameters

callback

The function to execute for each element, eventually taking three arguments:

currentValue

The current element being processed in the array.

currentIndex

The index of the current element being processed in the array.

listObj

The array thatforEach() is being applied to.

thisArgOptional

The value to use asthis when executingcallback.

Return value

None.

Example

In the following example we retrieve the list of classes set on a<pre> element as aDOMTokenList usingElement.classList. We when retrieve an iterator containing the valuesusingforEach(), writing each one to the<pre>'sNode.textContent inside theforEach() inner function.

HTML

html
<pre></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");

Result

Specifications

This feature does not appear to be defined in any specification.

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp