2

如何在knitr中突出显示表格的每一行?以下 .Rmd 的输出是一个全白表,而不是每隔一行灰色:

```{r setup, include=FALSE}
opts_chunk$set(cache=FALSE)
```
<style>
.myTable tr:nth-child(even) { /*(even) or (2n 0)*/
    background: #EEEEEE;
}
.myTable tr:nth-child(odd) { /*(odd) or (2n 1)*/
    background: #FFFFFF;
}
</style>

<TABLE border=1 class="myTable">
<TR> <TH>  </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH>  </TR>
  <TR> <TD align="right"> Mazda RX4 </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
  <TR> <TD align="right"> Mazda RX4 Wag </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
  <TR> <TD align="right"> Datsun 710 </TD> <TD align="right"> 22.80 </TD> <TD align="right"> 4.00 </TD> <TD align="right"> 108.00 </TD> </TR>
  <TR> <TD align="right"> Hornet 4 Drive </TD> <TD align="right"> 21.40 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 258.00 </TD> </TR>
  <TR> <TD align="right"> Hornet Sportabout </TD> <TD align="right"> 18.70 </TD> <TD align="right"> 8.00 </TD> <TD align="right"> 360.00 </TD> </TR>
   </TABLE>
4

1 回答 1

6
<style>
.myTable tr:nth-child(even) td{ 
    background: #EEEEEE;
}
.myTable tr:nth-child(odd) td { 
    background: #FFFFFF;}

</style>

您必须添加 td 以覆盖 body.td 声明的效果。

于 2013-05-21T06:18:42.567 回答