英文:
Javascript-function only changing 2 of 3 Elements
问题
以下是您要翻译的内容:
"So i wrote a fairly simple function to remove the "hidden" class from 3 items, when another element is clicked. Onclick it gets the 3 items right but it only changes item1+3 and item2 needs another click. I have absolutly no clue why this is happening.
https://codepen.io/zafire/pen/ExexWre
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function lmao() {
const collection = document.getElementsByClassName("hidden");
console.log(collection.length);
for (var i = 0; i < collection.length; i++) {
collection[i].classList.remove("hidden");
}
}
<!-- language: lang-css -->
body {
text-align: center;
font-size: 27px;
}
.hidden {
display: none;
}
.nothidden {
display: block;
}
<!-- language: lang-html -->
<div>
<span onclick="lmao()">arrow</span>
<br><br><br>
<span class="hidden">item1</span>
<br>
<span class="hidden">item2</span>
<br>
<span class="hidden">item3</span>
<br>
</div>
<!-- end snippet -->
Also i would like to notice solved this now differently but im still curious why this particular code does not work."
请注意,我已经去掉了代码部分,只翻译了文本。
英文:
So i wrote a fairly simple function to remove the "hidden" class from 3 items, when another element is clicked. Onclick it gets the 3 items right but it only changes item1+3 and item2 needs another click. I have absolutly no clue why this is happening.
https://codepen.io/zafire/pen/ExexWre
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function lmao() {
const collection = document.getElementsByClassName("hidden");
console.log(collection.length);
for (var i = 0; i < collection.length; i++) {
collection[i].classList.remove("hidden");
}
}
<!-- language: lang-css -->
body {
text-align: center;
font-size: 27px;
}
.hidden {
display: none;
}
.nothidden {
display: block;
}
<!-- language: lang-html -->
<div>
<span onclick="lmao()">arrow</span>
<br><br><br>
<span class="hidden">item1</span>
<br>
<span class="hidden">item2</span>
<br>
<span class="hidden">item3</span>
<br>
</div>
<!-- end snippet -->
Also i would like to notice solved this now differently but im still curious why this particular code does not work.
答案1
得分: 3
警告:这是一个实时的HTMLCollection。DOM中的更改将反映在数组中,随着更改的发生。如果此数组选择的元素不再符合选择器的条件,它将自动被移除。请注意这一点,以便进行迭代。
因此,当您从元素中删除类时,列表会受到影响。
您可以从最初的选择中创建一个数组
const collection = [...document.getElementsByClassName("hidden")];
或者您可以使用querySelectorAll
:
const collection = document.querySelectorAll(".hidden");
该方法说明:
querySelectorAll()
方法返回一个**静态(非实时)**NodeList,表示匹配指定选择器组的文档元素列表。
现在您可以无忧地进行迭代。
英文:
If you read the MDN docs of getElementsByClassName
> Warning: This is a live HTMLCollection. Changes in the DOM will reflect in the array as the changes occur. If an element selected by this array no longer qualifies for the selector, it will automatically be removed. Be aware of this for iteration purposes.
So when you remove the class from an element the list is affected.
You can create an array from the initial selection
const collection = [...document.getElementsByClassName("hidden")];
of you can use querySelectorAll
const collection = document.querySelectorAll(".hidden");
which states
> The Document method querySelectorAll()
returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
and now you can iterate over it with no worries.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论