Jak dodać klasę do każdego w div za pomocą pętli

0

Pytanie

To moja struktura, chcę dodać nieparzysty parzysty klasa w co dwie partycje, tak jak mogę utworzyć tę strukturę za pomocą pętli JavaScript, próbowałem wszystkiego, ale nic nie dostałem, studiuję cykl JavaScript, więc ktoś, proszę, pomóż mi z tym

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>

Potrzebna mi ta struktura:

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

Najlepsza odpowiedź

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

& jest побитовым "i". index & 2 byłoby 0 dla indeksy 0 i 1 i 2 dla indeksy 2 i 3 na przemian tak. 0 jest fałszywe i nie-0 to prawda. (Korzystanie z "parzystej liczby" i "nieparzystej" wydaje się być odwrotna, ale śledziłem korzystaniem.)

jQuery.each przyjmuje oddzwanianie, który może przyjmować zarówno indeks, jak i argument elementu.

2021-11-25 00:25:00

W innych językach

Ta strona jest w innych językach

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