如何使用 Div Id 选择器获取 table 列值

How to get table column values using Div Id selector

我有两个问题。 Table ID 在 DOM 中不可用。在以下情况下如何使用 Div id 获取第 3 行第 2 列的值“10”?

我在遍历第二列的以下代码中遇到错误。如何只获取第三行第二列?

Jquery代码

<script>
    $("'#part_li3_tt table'").children("tbody").children("tr").children("td:nth-child(2)").each(function () {
        alert("third row second col Value: " + $(this).html());
    });
</script>

HTML代码

<div id="part_li3_tt">
    <table>
        <tbody>
            <tr>
                <td>FName:</td>
                <td>Sara</td>
            </tr>
            <tr>
                <td>LName:</td>
                <td>Rodriguez</td>
            </tr>
            <tr>
                <td>Class:</td>
                <td>10</td>
            </tr>

        </tbody>
    </table>
</div>

您应该可以使用 jQuery 的 nth-of-type 选择器,如下所示:

alert($('#part_li3_tt table tr:nth-of-type(3) td:nth-of-type(2)').html());

但是,我不确定您为什么首先需要 ID part_li3_tt,因此您也许可以这样做:

alert($('table tr:nth-of-type(3) td:nth-of-type(2)').html());

如果您对 nth-of-type jQuery 选择器的工作原理感到困惑,您可以在这里阅读更多相关信息: https://api.jquery.com/nth-of-type-selector/