className only changing every other class

后端 未结 15 2197
陌清茗
陌清茗 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条回答
  •  萌比男神i
    2020-12-02 00:15

    You have already some good solutions.

    I think that the best one is the one from Rick Hitchcock.

    But a solution that I often use, to be safe when doing things like that, is to travel the collection backwards

    var nmax = blockSet.length - 1;
    for (var n=nmax; n>=0; n--) {
        blockSet[n].className = 'block-selected';
    }
    

    That isolates you from changes in the collection

提交回复
热议问题