Two other ways:

$('tr.className + tr').each(function(){
   var nxt = $(this), i=0;
   while (nxt.length){
     nxt.css('backgroundColor', i%2 ? 'white' : 'red');
     nxt = n.next(':not(.className)');
     i++;
   }
});

Or this (I prefer the first, this one is probably slower):

$('tr.className + tr').each(function(){
   var t = $(this), nxt = t;
   while (nxt.length){
     nxt = nxt.next(':not(.className)');
     t = t.add(next);
   }
   t.filter(':even').css('color','red');
   t.filter(:odd').css('color', 'white');
});

cheers,
- ricardo

On Feb 28, 12:06 am, Josh Powell <seas...@gmail.com> wrote:
> hmmm, tough one, it was fun.
>
> $(function() {
>     var color = '';
>     $('#everyother tr').each(function() {
>         if( $(this).hasClass('className') ) {
>             color = 'red';
>         } else if (color === 'red') {
>             $(this).css({'background-color': 'red'});
>             color = '';
>         } else {
>             color = 'red';
>         }
>     });
>
>     <table id="everyother">
> <tr class="className"><td>className</td></tr>
> <tr><td>1</td></tr>
> <tr><td>1</td></tr>
> <tr class="className"></><td>className</td></tr>
> <tr><td>1</td></tr>
> <tr><td>1</td></tr>
> <tr><td>1</td></tr>
> <tr class="className"><td>className</td></tr>
> <tr><td>1</td></tr>
> <tr><td>1</td></tr>
> </table>
>
> On Feb 27, 9:03 am, Magritte <marco.oliva...@gmail.com> wrote:
>
> > Ok, it work, but I another trouble.
>
> > the code now become:
> > $('tr:not(.className):odd').css({'background':'red'});
>
> > because I would like to color row alternatively.
> > The problem is that I would like that the alternativy start alway with
> > red.(first '<tr>' after every '<tr class="className"></></tr>' red,
> > then 'white', then red,....)
>
> > With this code it doesn't happen.
>
> > Thanks
>
> > On 27 Feb, 13:17, Thomas Jaggi <thomas.ja...@gmail.com> wrote:
>
> > > This should do it:
> > > $('tr:not(.className)').css({'background':'red'});

Reply via email to