2012-03-20 44 views
6

我有我的表设置为斑马条纹,但我如何完成使行颜色替代2行而不是单行?交替表行颜色,但与2行数据

我的数据标记看起来是这样的:

<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> 

我使用这条带是:

$(document).ready(function() { 
    $(".stripeMe tr").mouseover(function() { $(this).addClass("over"); }).mouseout(function() { $(this).removeClass("over"); }); 
    $(".stripeMe tr:even").addClass("alt"); 
}); 
+0

添加备用类你想要的风格的行。我怀疑你能够依靠css规则。 – 2012-03-20 20:41:08

回答

1

为什么不试试第n个孩子?这里有一些变化。 How nth-child works.我相信你可以在javascript中使用pseudo:hover而不是.mouseover。

6

像这样的东西应该工作:

$(".stripeMe tr").each(function(i){ 
    if (i%4 < 2){ 
     $(this).addClass("alt"); 
    } 
}); 
1

尝试使用.filter并获得index() % 3(($(this).index()+1) % 3 == 0)。请参见下面的代码,

DEMO

$(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'); 
}); 
0

使用jquerys n个子像这样:

$(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 
}); 
0

应该有CSS的方式来做到这一点,但如果你想的jQuery试试这个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; 
}​ 
4

要做到每两行进行条带化:

$('.stripeMe tr:nth-child(3n+3)').addClass('alt'); 
$('.stripeMe tr:nth-child(3n+4)').addClass('alt'); 
+3

我认为你的意思是4n + 3和4n + 4? – PerryJ 2013-03-19 21:49:27

+1

随着@ PerryJ的附录,我认为这是最简单和最好的答案。谢谢! – 2013-09-06 20:26:06