Javascript JQuery:选择三行

Javascript JQuery:选择三行,javascript,jquery,Javascript,Jquery,我有一张这样的桌子: <table> <tr> <th>h1</th> <th>h2</th> <th>h3</th> </tr> <tr> some content here </tr> <tr> <td>1</td> <td>2&

我有一张这样的桌子:

<table>
   <tr>
      <th>h1</th>
      <th>h2</th>
      <th>h3</th>
  </tr>

  <tr>
      some content here
  </tr>

  <tr>
      <td>1</td> <td>2</td> <td>3</td>
  </tr>
  <tr>
      inside this row, I have a table which contains 2 rows
  </tr>


   <tr>
      <td>1</td> <td>2</td> <td>3</td>
  </tr>
  <tr>
      inside this row, I have a table which contains 2 rows
  </tr>

   <tr>
      <td>1</td> <td>2</td> <td>3</td>
  </tr>
  <tr>
      inside this row, I have a table which contains 2 rows
  </tr>
  //it repeats like this


</table>
我希望从第三行开始使用jquery为每3行应用可选颜色,也就是说,我希望下面的代码部分具有可选颜色。我该怎么做?谢谢

      <tr>
         <td>1</td> <td>2</td> <td>3</td>
      </tr>
      <tr>
          inside this row, I have a table which contains 2 rows
      </tr>

基本上,我不想选择标题行和标题行之后的第一行。在那之后,我想每次选择3行,并给它另一种颜色。这3行将是如上所示的行。

使用第n个子选择器

$("table tr:nth-child(3n)")
您可以使用纯css3

table tr:nth-child(3n+3) { background:red; }
或者也可以通过jquery,比如kmd97-answer

$("table tr:nth-child(3n+3)").css("background","red");
3n表示每3个元素,+3表示从第3个元素开始


您可以试试这个

您的意思是要选择并更改恰好包含三个单元格的每一行的颜色?你的问题不完全清楚。NITYPICK:你应该使用ad和tbody元素。这会排除前2行、标题行和此处的某些内容行吗?你难道没有检查键入该行所需的时间吗?它将只选择第3行,而不是每3行,并使用edit only every 3元素,而不是从第3行开始,检查我的答案,3n+3
table tr:nth-child(3n+3) { background:red; }
$("table tr:nth-child(3n+3)").css("background","red");