使用 JS 更改所有 HTML 集合的样式

Change style to all HTMLcolection with JS

我有一个 HTML 集合,我想同时更改所有集合我怎么能在一行中做到这一点呢? imagenEnsaladas(列表项的 HMLT 集合)

imgSalad.src = carruselPhotos[positionCarrusel];
    carruselLi[0].style.backgroundColor = "#1a1d20";
    carruselLi[1].style.backgroundColor = "#1a1d20";
    carruselLi[2].style.backgroundColor = "#1a1d20";
    carruselLi[3].style.backgroundColor = "#1a1d20";
    carruselLi[4].style.backgroundColor = "#1a1d20";
    carruselLi[positionCarrusel].style.backgroundColor = "red";

你可以用 for 循环这样做

for (let i = 0; i < carruselLi.length; i++) {
if (i === positionCarrusel) {
    carruselLi[positionCarrusel].style.backgroundColor = "red"; 
} else {
    carruselLi[i].style.backgroundColor = "#1a1d20";
}

}