forEach()
方法在
DOMTokenList
interface calls the callback given in parameter once for each value pair in the list, in insertion order.
tokenList.forEach(callback [, thisArg]);
callback
currentValue
The current element being processed in the array.
currentIndex
The index of the current element being processed in the array.
listObj
forEach()
is being applied to.
thisArg
可选
this
when executing
callback
.
In the following example we retrieve the list of classes set on a
<span>
element as a
DOMTokenList
使用
Element.classList
. We when retrieve an iterator containing the values using
forEach()
, writing each one to the
<span>
's
Node.textContent
在
forEach()
inner function.
<span class="a b c"></span>
let span = document.querySelector("span");
let classes = span.classList;
let iterator = classes.values();
classes.forEach(
function(value, key, listObj) {
span.textContent += `${value} ${key}/${this} ++ `;
},
"arg"
);
This polyfill adds compatibility to all Browsers supporting ES5 :
if (window.DOMTokenList && !DOMTokenList.prototype.forEach) {
DOMTokenList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'forEach() (as iterable<Node>)' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
forEach
|
Chrome 45 | Edge 16 | Firefox 50 | IE 不支持 No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 50 | Opera Android 32 | Safari iOS 10 | Samsung Internet Android 5.0 |
完整支持
不支持
DOMSettableTokenList
(object that extends DOMTokenList with settable
.value
property)
DOMTokenList
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument