我有桌子表格中总是有多行包含selectrow="selectrow"之类的属性。例如,第三和第四行具有selectrow="selectrow"属性。现在,我想找出具有selectrow="selectrow"属性的最后一行的索引。我不想使用每个。我寻找这样的解决方案:

$("table > tbody > tr[selectrow='selectrow']:last-child").index();


这是html:

<table>
 <tbody>
  <tr >
  </tr>
  <tr>
  </tr>
  <tr selectrow="selectrow">
  </tr>
  <tr selectrow="selectrow">
  </tr>
  <tr>
  </tr>
  <tr>
  </tr>
 </tbody>
</table>


在这个例子中我想得到4。

最佳答案

尝试以下操作:使用:last将为您提供最后一个匹配的选择。 :last-child将选择表的最后一个子项,并尝试匹配其他选择条件(在您的情况下为selectrow='selectrow'),如果未找到匹配项,它将重新运行-1。

$("table > tbody > tr[selectrow='selectrow']:last").index();


JSfiddle Demo

有关更多信息
:last
:last-child

09-25 20:03