className only changing every other class

后端 未结 15 2240
陌清茗
陌清茗 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-01 23:58

    By assigning a value to .className you overwrite every class on that element. What you might want to take a look at is the .classList attribute.

    Remove a class:

    blockSet[0].classList.remove('block-default');
    

    Add the new class:

    blockSet[0].classList.add('block-selected');
    

    A good point to start with, when your trying to do stuff, jQuery usually did for you, is http://youmightnotneedjquery.com/

提交回复
热议问题