简体   繁体   English

如何在表格中找到最匹配的相邻行

[英]How to find closest matching adjacent row in a table

Using Jquery, how can I find the closest adjacent row matching a pattern.使用 Jquery,如何找到与模式匹配的最近相邻行。

I have a table of rows, where one row represents the header of the following rows until the next header.我有一个行表,其中一行代表以下行的标题,直到下一个标题。 I have a click handler from an element within one of the rows.我有一个来自其中一行元素的点击处理程序。 Starting from the element clicked, how can I find the nearest header row?从单击的元素开始,如何找到最近的标题行?

I tried .closest(".header") but there's no results because it's not a parent.我试过.closest(".header")但没有结果,因为它不是父级。 I also can't look for .parent().closest(".header") as there are many rows matching that description.我也找不到.parent().closest(".header")因为有很多行匹配该描述。

...
<tr class="header"></tr>
<tr></tr>
<tr></tr> // <-- starting here, find tr above with class "header"
<tr></tr>
<tr class="header"></tr>
<tr></tr>
<tr></tr>
<tr></tr>
...

I am developing an extension and can not change the html.我正在开发一个扩展,无法更改 html。

.prev将在前面的兄弟姐妹中搜索,因此您可以使用它并选择集合中的最后一个:

const prevHeader = $(theTr).prev('.header').last();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM