JavaScript函数仅更改三个元素中的2个。

huangapple go评论62阅读模式
英文:

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(&quot;hidden&quot;);
  console.log(collection.length);
  for (var i = 0; i &lt; collection.length; i++) {
    collection[i].classList.remove(&quot;hidden&quot;);
  }
}

<!-- language: lang-css -->

body {
  text-align: center;
  font-size: 27px;
}

.hidden {
  display: none;
}

.nothidden {
  display: block;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;span onclick=&quot;lmao()&quot;&gt;arrow&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;&lt;br&gt;
  &lt;span class=&quot;hidden&quot;&gt;item1&lt;/span&gt;
  &lt;br&gt;
  &lt;span class=&quot;hidden&quot;&gt;item2&lt;/span&gt;
  &lt;br&gt;
  &lt;span class=&quot;hidden&quot;&gt;item3&lt;/span&gt;
  &lt;br&gt;
&lt;/div&gt;

<!-- 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(&quot;hidden&quot;)];

of you can use querySelectorAll

const collection = document.querySelectorAll(&quot;.hidden&quot;);

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.

huangapple
  • 本文由 发表于 2023年2月14日 21:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定