How to change the style of elements with same class name

后端 未结 3 1775
挽巷
挽巷 2020-12-18 15:43

Using javascript, I want to change the style of class .s into

.s {
  display: block;
}

Why this cannot work:

         


        
3条回答
  •  庸人自扰
    2020-12-18 16:06

    As getElementsByClassName returns an array you need to make a for loop over all found elements:

    var elements = document.getElementsByClassName('s');
    for (var i = 0; i < elements.length; i++) {
        elements[i].style.display = "block";
    }
    

提交回复
热议问题