className only changing every other class

后端 未结 15 2221
陌清茗
陌清茗 2020-12-01 23:41

I\'m performing a small text with JavaScript with the getElementsByClassName() and I am getting some unwanted results. I would like the script to change each CS

15条回答
  •  臣服心动
    2020-12-02 00:02

    This worked for me

    var blockSet = document.getElementsByClassName("block-default");
        var blockSetLength = blockSet.length;
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
     blockSet[0].className = "block-selected";
    
       
    .block-default {
        width: 100px;
        height:50px;
        background-color: green;
        border: 1px solid red;
        padding:10px;
    }
    
    .block-selected {
        width: 100px;
        height:50px;
        background-color: blue;
        border: 1px solid white;
        padding:10px;
     }
    BLOCK1
    BLOCK2
    BLOCK3
    BLOCK4
    BLOCK5
    BLOCK6
    BLOCK7
    BLOCK8

提交回复
热议问题