如何选择与另一个表格单元格有某种关系的某个表格单元格

How to select a certain table cell in certain relation to another?

本文关键字:表格 单元格 关系 另一个 何选择 选择      更新时间:2023-09-26

我有

<table>
    <tbody>
        <tr>
            <th colspan="2">Infobox</th>
        </tr>
        <tr>
            <td>Lorem ipsum ...</td>
            <td>Lorem ipsum ...</td>
        </tr>
        ⁞
        <tr>
            <td>Lorem ipsum ...</td>
            <td>Lorem ipsum ...</td>
        </tr>
        <tr>
            <th colspan="2">[random stuff]</th>
        </tr>
        <!-- *** Where I want to add another row *** -->
        <tr>
            <td>Lorem ipsum ...</td>
            <td>Lorem ipsum ...</td>
        </tr>
        ⁞

选择和附加附加行的最优雅/最有效的方法是什么?我从

$("table th:contains('Infobox')").closest('tbody th[colspan="2"]');

但这并不能选择我想要的。感谢您的帮助和快乐的圣诞:o)

.after呢?

$("table th:contains('Infobox')").after("<tr></tr>")

这将在选择器之后附加 HTML。

您当前的查询正在尝试使用 closest 在 DOM 中上下搜索。 closest只搜索祖先

您希望如何定位正确的表有点模糊,但按照您的示例,您将需要这样的东西:http://jsfiddle.net/TrueBlueAussie/y80gzL4k/2/

 $('table th:contains("Infobox")')
    .closest('tbody')
    .find('tr:has(th[colspan="2"]):last')
    .after("<tr><th>New row goes here</th></tr>");