0

I have in a div Container some News Articles with and without images. Now want to add for each article without image a different class. If there is an image, the image is wrapped with <div class="news-img-wrap"></div>. Articles with no images don't have this div. So i try this

$('.news-list-view article .content-background').each(function () {
   if ($(this).find('.news-img-wrap').length) {
      $(this).addClass('colored-box-red ');



   } else {
      $(this).addClass('default-box');
   }
});

This code works fine for all articles with image get the extra class. My Question is how i can give each article with image an different extra class "first,second,third"

My example till now.http://jsfiddle.net/b9JuT/

thanks for your help and sorry for my bad english.

4

1 回答 1

0

您可以使用计数并通过在每次添加类时递增它来动态创建唯一类:

var classCount = 1;

$('.news-list-view article .content-background').each(function () {
if ($(this).find('.news-img-wrap').length) {
    $(this).addClass('colored-box-red ').addClass('img-' + classCount);
     classCount++;
} else {
    $(this).addClass('default-box');
}
});
于 2013-05-18T02:24:50.537 回答