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 badges3 Answers
Reset to default 10Collections 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
版权声明:本文标题:javascript - forEach processes array of DOM elements through an one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744293109a2599209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论