className only changing every other class

后端 未结 15 2239
陌清茗
陌清茗 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:07

    document.getElementsByClassName returns a HTMLCollection object, which is live

    An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.

    So when you call

    blockSet[0].className = "block-selected";
    

    You changed the underlying document and that item is not in the collection anymore (the blockSet[0] is now the second item in your original selection).

提交回复
热议问题