最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Strange behavior when iterating over HTMLCollection from getElementsByClassName - Stack Overflow

programmeradmin1浏览0评论

I wrote a function to change the class of elements to change their properties. For some reason, only some of the elements have changed. It took me a few hours to find a solution, but it seems odd to me. Perhaps you can explain this to me.

This isn’t working:

function replace(){
  var elements = document.getElementsByClassName('classOne');

  for (var i = 0; i < elements.length; i++) {
    elements[i].className = 'classTwo';               
  }
}

See the JSFiddle: only every second item is affected; only every second red element changes color to blue.

So I changed the final expression of the for loop to not increment i anymore:

function replace(){
  var elements = document.getElementsByClassName('classOne');

  for (var i = 0; i < elements.length; i) { // Here’s the difference
    elements[i].className = 'classTwo';               
  }
}

This works well! It seems as though push is called and no increment is needed. Is this normal? It is different from the examples I’ve seen.

I wrote a function to change the class of elements to change their properties. For some reason, only some of the elements have changed. It took me a few hours to find a solution, but it seems odd to me. Perhaps you can explain this to me.

This isn’t working:

function replace(){
  var elements = document.getElementsByClassName('classOne');

  for (var i = 0; i < elements.length; i++) {
    elements[i].className = 'classTwo';               
  }
}

See the JSFiddle: only every second item is affected; only every second red element changes color to blue.

So I changed the final expression of the for loop to not increment i anymore:

function replace(){
  var elements = document.getElementsByClassName('classOne');

  for (var i = 0; i < elements.length; i) { // Here’s the difference
    elements[i].className = 'classTwo';               
  }
}

This works well! It seems as though push is called and no increment is needed. Is this normal? It is different from the examples I’ve seen.

Share Improve this question edited Aug 17, 2019 at 8:27 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Mar 22, 2013 at 3:58 MeNaMeNa 1,48710 silver badges23 bronze badges 2
  • I have never heard the term “loop coefficient”. JavaScript calls the last part of a for loop the “final expression” (or “increment” in certain contexts of the spec) and i++ an “increment” (or, generalized, “UpdateExpression” in the spec). – Sebastian Simon Commented Aug 17, 2019 at 8:35
  • Related: for...in loop not looping through all properties?. – Sebastian Simon Commented Nov 17, 2022 at 2:40
Add a comment  | 

3 Answers 3

Reset to default 21

What's going on is an odd side effect. When you reassign className for each element of elements, the element gets removed from the array! (Actually, as @ user2428118 points out, elements is an array-like object, not an array. See this thread for the difference.) This is because it no longer has the classOne class name. When your loop exits (in the second case), the elements array will be empty.

You could write your loop as:

while (elements.length) {
    elements[0].className = 'classTwo'; // removes elements[0] from elements!
}

In your first case, by incrementing i, you are skipping half of the (original) elements that have class classOne.

Excellent question, by the way. Well-researched and clear.

getElementsByClassName returns a NodeList. A NodeList collection is a live collection, which means that the modification of the document affects the collection. more

Or revert the loop, beginning by length-1 and step down to 0

发布评论

评论列表(0)

  1. 暂无评论