Jquery 使用类查找上一个表行
我的桌子看起来像这样:Jquery 使用类查找上一个表行,jquery,Jquery,我的桌子看起来像这样: <table> <tr>...</tr> <tr class="gotChild">...</tr> <tr class="gotParent">...</tr> <tr>...</tr> <tr>...</tr> <tr class="gotChild">...</tr>
<table>
<tr>...</tr>
<tr class="gotChild">...</tr>
<tr class="gotParent">...</tr>
<tr>...</tr>
<tr>...</tr>
<tr class="gotChild">...</tr>
<tr class="gotParent">...</tr>
<tr class="gotParent">...</tr>
<tr class="gotParent">...</tr>
<tr class="gotParent">...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr class="gotChild">...</tr>
<tr class="gotParent">...</tr>
<tr class="gotParent">...</tr>
</table>
但是,如果有更多的.gotParent
行与前面的行相同,则此函数不起作用
使用.prevAll()
会影响所有.gotChild
行(只应是它找到的第一行)
使用.prevUntill()
会影响所有表行,在这些行中,它只会影响.gotChild
行
如何定位此特定行?您可以使用下面的代码选择最近的上一个兄弟姐妹:
var gotChild = $(this).parent().parent().prevAll(".gotChild:first");
将只选择第一个匹配值类似于此的
$(this).parent().parent().prevAll(“.gotChild:first”);
prevAll(.gotChild:first”)
成功了。谢谢@ValeryViktorovsky。将其添加为答案,我会接受它。太好了!我添加了答案。
var gotChild = $(this).parent().parent().prevAll(".gotChild:first");
$(this).prevAll(".gotChild").eq(0);