javascript - Changing table row color per two rows - Stack Overflow

I have an HTML table and would like to change the row color every two rows, for two rows at a time. I d

I have an HTML table and would like to change the row color every two rows, for two rows at a time. I don't mean selecting every other row using nth-child(even), which is all I keep seeing in my search engine results. I'm trying to set the background color of rows 1 and 2, then 5 and 6, then 9 and 10...and so on, leaving the other rows with the default white background.

I've tried using :nth-child(2n+b) and I don't think it's possible using that pseudo-selector. The tables are dynamically generated and will fluctuate in row count, so hard coding is not an option.

Is there some sort of JavaScript way to do this?

I have an HTML table and would like to change the row color every two rows, for two rows at a time. I don't mean selecting every other row using nth-child(even), which is all I keep seeing in my search engine results. I'm trying to set the background color of rows 1 and 2, then 5 and 6, then 9 and 10...and so on, leaving the other rows with the default white background.

I've tried using :nth-child(2n+b) and I don't think it's possible using that pseudo-selector. The tables are dynamically generated and will fluctuate in row count, so hard coding is not an option.

Is there some sort of JavaScript way to do this?

Share Improve this question edited May 27, 2014 at 21:16 lonesomeday 238k53 gold badges327 silver badges328 bronze badges asked May 27, 2014 at 21:01 abahlerabahler 991 silver badge13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It's definitely possible using CSS's :nth-child() notation:

tr:nth-child(4n + 1) td,
tr:nth-child(4n + 2) td {
    background-color: #ffa;
}

tr:nth-child(4n + 3) td,
tr:nth-child(4n + 4) td {
    background-color: #f90;
}

JS Fiddle demo.

This is the solution for your problem in php. Hope you can convert it into your required javascript.

$n = "#666";
$c = 0;
$p = 0;
while($c < 9){
if($p%2 == 0){
$n = ($n == "#666")?"#FFF":"#666";
$p=0;
}
$c++;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208399a4616701.html

相关推荐

  • javascript - Changing table row color per two rows - Stack Overflow

    I have an HTML table and would like to change the row color every two rows, for two rows at a time. I d

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信