Reputation: 470
I've got my table setup for Zebra striping, but how do I accomplish making the row color alternate for 2 rows instead of a single row?
My data markup looks like this:
<tr>
<td>@task.TaskNum</td>
<td>@task.RepiarTime</td>
<td>Priority Club</td>
<td>SD</td>
<td>Commercial</td>
<td>Reg Commercial</td>
<td>After Hours</td>
</tr>
<tr><td colspan="7">
@task.Description.ToString()
</td></tr>
I am using this to stripe it:
$(document).ready(function () {
$(".stripeMe tr").mouseover(function () { $(this).addClass("over"); }).mouseout(function () { $(this).removeClass("over"); });
$(".stripeMe tr:even").addClass("alt");
});
Upvotes: 6
Views: 3396
Reputation: 5253
To do striping every two rows:
$('.stripeMe tr:nth-child(3n+3)').addClass('alt');
$('.stripeMe tr:nth-child(3n+4)').addClass('alt');
Upvotes: 4
Reputation: 36642
Use jquerys nth child like this:
$(document).ready(function () {
$(".stripeMe tr").mouseover(function () { $(this).addClass("over"); }).mouseout(function () { $(this).removeClass("over"); });
$(".stripeMe tr:nth-child(3n)").addClass("alt"); // 3n selects every third element
});
Upvotes: 0
Reputation: 207890
There should be a way in CSS to do this, but if you want jQuery try this jsFiddle example.
jQuery:
var index = 0;
$('tr').each(function() {
$(this).addClass((index <= 1) ? 'odd' : 'even');
index++;
if (index == 4) index = 0;
});
CSS:
.odd {
background: #999;
}
.even {
background: #ddd;
}
Upvotes: 0
Reputation: 79830
Try using .filter
and get the index() % 3
or (($(this).index()+1) % 3 == 0)
. See code below,
$(document).ready (function () {
$('#myTable tr').filter(function () {
//or (($(this).index()+1) % 3 == 0) if you want 3rd row
//to be highlighted first.
return ($(this).index() % 3 == 0);
}).addClass('alt');
});
Upvotes: 1
Reputation: 22830
Something like this should work:
$(".stripeMe tr").each(function(i){
if (i%4 < 2){
$(this).addClass("alt");
}
});
Upvotes: 6
Reputation: 195
Why not try nth-child? Here are a bunch of variations here. How nth-child works. I'm sure you can use the pseudo :hover instead of .mouseover in javascript.
Upvotes: 1