获取 class 下的最后一个 tr

Get the last tr under the class

我怎样才能得到 class group 之后的最后一个 tr 像这样

<table id="compareTable">
  <tr class="group"><td>a</td></tr>
  <tr><td>b</td></tr>
  <tr><td>c</td></tr>
  <tr><td>One</td></tr> <!--Get this one-->
  <tr class="group"><td>a</td></tr>
  <tr><td>b</td></tr>
  <tr><td>c</td></tr>
  <tr><td>two</td></tr> <!--Get this two-->
</table>

View Demo Here

与jquery无关。主要是在domselect或者.

使用下面的代码,我们可以 select 特定 class 之后的最后一个 tr。

table#compareTable .group ~ tr:last-类型

Working Fiddle.

可以使用 nextUntil 来完成,例如 :

$('#compareTable tr.group').each(function(){
  $(this).nextUntil( "tr.group", "tr" ).last().css( "font-weight", "bold" );
});

$('#compareTable tr.group').each(function(){
  $(this).nextUntil( "tr.group", "tr" ).last().css( "font-weight", "bold" );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="compareTable">
  <tr class="group">
    <td>a</td>
  </tr>
  <tr>
    <td>b</td>
  </tr>
  <tr>
    <td>c</td>
  </tr>
  <tr>
    <td>One</td>
  </tr>
  <!--Get this one-->
  <tr class="group">
    <td>a</td>
  </tr>
  <tr>
    <td>b</td>
  </tr>
  <tr>
    <td>c</td>
  </tr>
  <tr>
    <td>two</td>
  </tr>
  <!--Get this two-->
</table>