How do I change multiple elements with same class name using Javascript?

后端 未结 6 975
无人共我
无人共我 2020-12-21 03:45

Follow up to my previous question - I want to use a button to show / hide multiple elements with the same class name using JS, yet it appears that I can only change the firs

6条回答
  •  囚心锁ツ
    2020-12-21 04:31

    var X = document.querySelectorAll(".className");
    
    for (var i = 0; i < X.length; i++) {
    
        X[i].style.cssProperty = someNumber + "unitType";
    
    }
    

    Example: X[i].style.width = 100 + "%";

    If you want it to apply random integers:

    var X = document.querySelectorAll(".className");
    
    for (var i = 0; i < X.length; i++) {
    
        var rand = Math.floor(Math.random() * someNumber);
    
        X[i].style.cssProperty = rand + "unitType";
    
    }
    

提交回复
热议问题