Javascript - 如何循环遍历getElementsByClassName返回的所有DOM元素
html代码示例:
<div class='first'>First</div>
<div class='first'>First 111</div>
<div class='second'>Second</div>
1
2
3
2
3
js代码示例:
let elements = document.getElementsByClassName('first');
Array.prototype.forEach.call(elements, function (element) {
ele.classList.remove("111");
element.innerHTML = '111';
});
//OR
Array.from(document.getElementsByClassName("first")).forEach((ele) => {
ele.classList.remove("111");
element.innerHTML = '111';
});
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13