我知道在一个for-of
循环中,可以使用该Array.entries()
方法。正如概述的那样,这通常很好用 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries。
但是,如果我尝试执行以下操作:
for (const [i, foo] of document.getElementsByClassName('foo').entries())
{
console.log(`i = ${i}, foo = ${foo}`);
}
我被告知:
Uncaught TypeError: document.getElementsByClassName.entries is not a function or its return value is not iterable
我知道我可以使用一个很好的旧常规 for 循环......但是:
- 为什么它不起作用?
- 我是不是误会了什么?
- 我可以让它按照我想要的方式工作(除了使用常规的 for 循环)吗?
我最好的猜测是 anHTMLCollection
不是标准数组,因此没有这样的数字索引......