检索受“rowspan”影响的行的列索引的最有效方法是什么?
考虑下表:
<table>
<thead>
<tr>
<th scope="col" />
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">C</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Apples</td>
<td>Oranges</td>
<td>Pears</td>
</tr>
<tr>
<th scope="row">2</th>
<td rowspan="2">Subcategory Heading</td>
<td>ASP.Net</td>
<td>Other</td>
</tr>
<tr>
<th scope="row">3</th>
<td>MVC</td>
<td>PHP</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Red</td>
<td>Green</td>
<td>Blue</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">Some pointless footer content</td>
</tr>
</tfoot>
</table>
如何使用 jQuery 检索包含文本“MVC”或“PHP”的单元格的正确列号?正确的答案是第 3 列,但仅使用 .prevAll().length
只会计算先前单元格的数量,在这种情况下这不会准确反映它们的实际列位置?
我确信我需要使用each(),但我也在寻找最有效的实现。
Consider the following table:
<table>
<thead>
<tr>
<th scope="col" />
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">C</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Apples</td>
<td>Oranges</td>
<td>Pears</td>
</tr>
<tr>
<th scope="row">2</th>
<td rowspan="2">Subcategory Heading</td>
<td>ASP.Net</td>
<td>Other</td>
</tr>
<tr>
<th scope="row">3</th>
<td>MVC</td>
<td>PHP</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Red</td>
<td>Green</td>
<td>Blue</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">Some pointless footer content</td>
</tr>
</tfoot>
</table>
How would I retrieve the correct column number for the cell containing the text "MVC" or "PHP" using jQuery? The correct answer would be column 3, but just using .prevAll().length
will only count the number of previous cells, which will not accurately reflect their actual column position in this case?
I'm sure I'll need to use each()
, but I'm looking for the most efficient implementation as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最近针对同样的问题为这种情况编写了一个解决方案:
如何使用 jQuery 找到每个表格单元格的“视觉位置”?
对于搜索此内容的人来说,可能是另一种解决方案。
I recently wrote a solution for this situation for the same problem:
How can I find each table cell's "visual location" using jQuery?
Might be another solution for people searching for this.
像这样的东西吗?
Something like this?
我认为最接近缩短搜索步骤的方法是这样做:
I think the closest to shorten search steps would do something like this:
尝试这样的事情,你就明白了。它有效,所以为什么不呢:)
感谢 这个人在 我的问题。
Try something like this, you get the point. It works, so why not :)
Give kudos to this guy for his answer on my quesiton.
那么,最短的(过度)简化是:
注意,返回的索引是从零开始的。基本上,首先你找到感兴趣的细胞。对于该列,您在 DOM 中上一层到
,获取所有子级(在本例中将是后代
)和
)并找到该集合中感兴趣的元素的位置。对于该行,在本例中为
,获取所有行,并找到
的行位置的兴趣。
正如您可以想象的那样,表结构的变化会改变可能的结果。但是,如果您稍后以与获取索引相同的方式以编程方式使用索引,则可以返回到同一单元格。我提出这一点是因为如果您以某种方式在 UI 中直观地使用索引,您可能需要变得更加棘手并考虑跨度,
与没有
等...
不知道它是否比meder的答案更有效。但是,它绝对更易于维护! :)
Well, the shortest (over)simplification is:
Note, the indexes returned are zero-based. Basically, first you find the cell of interest. For the column, you go up a level in the DOM to the
<tr>
, get all children (which in this case will be the descendant<td>
s and the<th>
s) and find the position of the element of interest in that set. For the row, you climb up to, in this case, the<tbody>
, get all rows, and find the position of the row of the<td>
of interest.As you can imagine, changes in the structure of the table changes the possible results. However, if you programmatically use the indexes later the same way you got them, you can get back to the same cell. I bring this up because if you somehow use the indexes visually in the UI, you may have to get trickier and account for spans,
<tbody>
vs no<tbody>
, etc...Don't know if it is more efficient than meder's answer. But, it is definitely much more maintainable! :)