Как добавить класс в нечетный класс «recensie» в разделе с классом ‘album_reviews’

#jquery

#jquery

Вопрос:

Итак, у меня есть этот вопрос.

Мне интересно, как я могу добавить класс «selected» к каждому нечетному классу с именем recensie в разделе только с классом album_reviews.

 <section class="album_reviews">
    <h2 class="sectiontitel"> Album Reviews </h2>
    <article class="recensie">
      <h1> Neil Young – A Letter Home </h1>
      <img src="Images/Albums/A_Letter_Home.jpg" alt="album_image"/>
      <p>Earlier this year, Neil Young unveiled Pono, a super-high-def audio service meant to deliver us from the sonic crimes of the earbud era. For his next act, he's released an acoustic covers set recorded at Jack White's Nashville music shop on a Voice-O-Graph--a super-low-def 1940s contraption that looks like a phone booth and sounds a few steps removed from a rusty tin can and some twine. If it's meant as some kind of joke, here's the punch line: In its perverse way, A Letter Home is one of the most enjoyable records Young has made this century.</p>
      <p> 
        <a href="http://www.rollingstone.com/music/albumreviews/a-letter-home-20140502#ixzz33g0Hq3pw"> Read more </a>
      </p>       
    </article>
  </section>

  <section class="band_members">
    <h2 class="sectiontitel"> Band Members</h2>
    <article class="recensie">
      <h1> Neil Young </h1>
      <figure>
          <img src="Images/Artists/Neil_Young.jpg" alt="Macaque in the trees">
          <figcaption> Neil Young <br /> Singer Songwriter </figcaption>
      </figure>
    </article>
  </section>
  

Это мой HTML-код.
Я знаю, как добавить класс только к нечетным, но не только к коэффициентам в разделе.album_reviews

Надеюсь, кто-нибудь может помочь

Ответ №1:

Вот так:

 $('.album_reviews .recensie:odd').addClass('selected');
  

Ответ №2:

 $('.album_reviews').find('.recensie:odd').addClass('selected');
  

ДЕМОНСТРАЦИЯ

Комментарии:

1. На самом деле я уже нашел ответ, но спасибо. Как бы вы ни были наиболее близки к тому, что мне было нужно 🙂 $(«section.album_reviews»).find(«article.recensie: odd»).addClass(«выбранный»);

Ответ №3:

Используйте :odd селектор в jquery. Он выбирает все нечетные элементы

 $("section.album_reviews .recensie:odd").addClass('selected');
  

Комментарии:

1. Не могли бы вы дать небольшое объяснение того, как работает ваш ответ?