JavaScript HtmlCollection loop never returns second element

做~自己de王妃 提交于 2019-12-02 08:42:38

getElementsByClassName returns a live HTMLCollection.

This line:

activeTabSheet.className.replace('tabSheetActive', 'tabSheet');

Stops the first item in the list from being a member of the class. Consequently it is removed and everything else is shuffled down (so the element that was at index 1 moves to index 0).


To deal with this you can:

  • Use querySelectorAll which returns a non-live NodeList
  • Loop over the HTMLCollection backwards
  • Use a while loop, test the length of the HTMLCollection, and always modify index 0.
  • Copy all the values into an array before looping over that
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!