jquery - 检索受 "rowspan"影响的行的列索引的最有效方法是什么?

标签 jquery algorithm html-table traversal

考虑下表:

<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 只会计算之前单元格的数量,在这种情况下不能准确反射(reflect)它们的实际列位置?

我确定我需要使用 each(),但我也在寻找最有效的实现方式。

最佳答案

我最近针对同样的问题写了一个针对这种情况的解决方案:

How can I find each table cell's "visual location" using jQuery?

可能是搜索此内容的人的另一种解决方案。

关于jquery - 检索受 "rowspan"影响的行的列索引的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452555/

相关文章:

jQuery/CSS Gallery/Slideshow 如何限制缩略图滚动到可见区域

javascript - 如何更改 chromes 日期解析/排序以匹配 ff/ie 日期解析?

c# - 如何在 C# 中为 "decimal to binary"修复此代码

algorithm - 对于树的每个节点,找到最近的祖先节点,使得 val[node] 与 val[ancestor] 互质

html - 具有固定宽度 <thead> 的 HTML 表格中的两级行?

jquery - 为什么 jQuery 对于 $.attr 'open' 总是返回 ('open' ),而不管属性的实际值是什么?

javascript - 我如何让用户等到 session 组织者先加入后才能加入

algorithm - 两个移动矩形之间的最小距离

html - gmail 应用程序中 HTML 电子邮件中不需要的行

google-chrome - Chrome 53 在第二页和以后的页面上两次打印表头的解决方法?