2015-04-21 40 views
0

我有一張桌子。表中總是有多行包含selectrow="selectrow"之類的屬性。例如第3行和第4行有selectrow="selectrow"屬性。現在我想找出具有selectrow="selectrow"屬性的最後一行的索引。我不想使用每個。我要尋找的解決方案是這樣的:找到最後的html元素與jQuery的特殊屬性

$("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

+0

所以''(「table> tbody> tr [selectrow ='selectrow']:last-child」)有什麼問題。 –

+0

,它返回表中最後一行的索引,而另一個詞則忽略該屬性。 – adib16

+1

我以爲它返回了'-1',因爲沒有具有該屬性的元素也是父級的最後一個孩子? – adeneo

回答

4

試試這個:使用:last這將給你最後的匹配的選擇。 :last-child將選擇表的最後一個孩子,並嘗試匹配其他選擇標準(在您的情況下爲selectrow='selectrow'),並且當找不到匹配時,它將重試-1。在
:last
:last-child

+0

非常感謝你 – adib16

+0

高興地幫助你:) –

1

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

JSfiddle Demo

更多信息試試這個

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

它將返回上一個tr的對象。

相關問題