Jak PŘIDAT třídu v každé div pomocí smyčky

0

Otázka

Tohle je můj strukturu, chci přidat i zvláštní třídy, v každé dva prvky div tak, jak mohu dosáhnout této struktury pomocí Javascriptu smyčky zkoušel jsem všechno, ale nemám nic, učím se JavaScript smyčky, takže někdo, prosím, pomozte mi s tím

var i = 0;
$('.CollectionInner__Products .Grid__Cell .ProductItem').each(function(i) {
  var index = 0;
  if (index % 3 == 0) {
    $(this).addClass("odd");
  }
});
<div class="custompsps">
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>

</div>
<div class="custompsps">
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>
  <div class="ProductItem">
  </div>

</div>

Chci tuto strukturu:

i want this stucture
<div class="custompsps">
  <div class="ProductItem even">
  </div>
  <div class="ProductItem even">
  </div>
  <div class="ProductItem odd">
  </div>
  <div class="ProductItem odd">
  </div>

</div>
<div class="custompsps">
  <div class="ProductItem even">
  </div>
  <div class="ProductItem even">
  </div>
  <div class="ProductItem odd">
  </div>
  <div class="ProductItem odd">
  </div>

</div>
css html javascript jquery
2021-11-24 03:47:49
1

Nejlepší odpověď

1
$('.CollectionInner__Products .Grid__Cell .ProductItem').each(function(index, element) {
  $(element).addClass(index & 2 ? "even" : "odd");
});

& je bitové "a". index & 2 by být 0 pro index 0 a 1, a 2 pro index 2 a 3, střídavě, jako je tento. 0 je falsy a non-0 je pravdivý. (Vaše používání "i" a "liché" zdát dozadu, ale sledoval jsem vaše použití.)

jQuery je .each přijímá callback, která může trvat i indexem a prvek argument.

2021-11-25 00:25:00

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................