admin管理员组

文章数量:1401167

I encountered a strange behavior of simple JS code. Elements processed through an one.

var a = document.getElementsByClassName('nahren');
Array.prototype.forEach.call(a, function(e) {
    e.classList.remove('nahren')
})

Example on JSFiddle

I encountered a strange behavior of simple JS code. Elements processed through an one.

var a = document.getElementsByClassName('nahren');
Array.prototype.forEach.call(a, function(e) {
    e.classList.remove('nahren')
})

Example on JSFiddle

Share Improve this question asked Nov 2, 2016 at 4:57 Kesantielu DasefernKesantielu Dasefern 2761 gold badge3 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Collections returned by getElementsByClassName are "live": if you change them to disqualify them from the selector, they disappear from the collection. This makes iterating over them wonky. Use querySelectorAll('.nahren'), which returns a "dead" collection, or pin the "live" collection by cloning it:

var deadArray = Array.prototype.slice.call(liveCollection);
deadArray.forEach(...)

Amadan, has already answered correctly, but here is another implementation in which you can clone them using ES6.

var a = Array.from(document.getElementsByClassName('nahren')); //clone array
Array.prototype.forEach.call(a, function(e,i) {
  e.classList.remove('nahren');
})
div#container div {
  height: 30px;
  width: 100px;
  margin-bottom: 5px;
}

.hren {
  outline: 1px red solid;
}

.hren.nahren {
  outline: 1px blue solid;
}
<div id="container">
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
<div class="hren nahren"></div>
  <div>

I have attached the code snippet for your requirement. In your case you cannot use class name 'nahren' as the selector. Because on removing the class 'nahren' once from your DOM element, your object is not the same any more. You could use the 'hren' class for selecting the DOM object, since you are removing the 'nahren' class only your DOM selector will not change.

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      div {
        height: 30px;
        width: 100px;
        margin-bottom: 5px;
      }
      
      .hren {
        outline: 1px red solid
      }
      
      .hren.nahren {
        outline: 1px blue solid
      }
    </style>
    <script>
	function RemoveClass(){
		var a = document.getElementsByClassName('hren');
	    Array.prototype.forEach.call(a, function(e,i) {
	       e.classList.remove('nahren');
	    })
	}
          
    </script>
  </head>
  <body>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
    <div class="hren nahren"></div>
	<button onclick="RemoveClass()">Remove</button>
  </body>
</html>

本文标签: javascriptforEach processes array of DOM elements through an oneStack Overflow