0
var list = document.getElementsByClassName("sidebar-holder");
console.log(list);// html collection of length 2
console.log(list[0]);// undefined

在这里,我运行了以下代码,并使用了console.log()结果的注释。

虽然具有该类名称的元素正在异步呈现,但我认为这不是问题,因为第一个console.log()呈现预期结果。第二个没有。

4

1 回答 1

0

添加Array.from(list)以将 HTML 元素转换为数组,然后才能对其进行迭代。之所以如此,是因为 HTML 元素是类数组但不是纯数组。

于 2020-08-03T02:54:15.707 回答