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

时间:2016-08-19 17:21:18

标签: jquery selector

我有两个问题。表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>

1 个答案:

答案 0 :(得分:2)

您应该可以使用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/