jQuery只使用find()方法查找表的第二个tr及其子元素

时间:2017-10-25 03:33:14

标签: jquery

<table>
    <tr></tr>
    <tr>
        First Want to select this tr
        <td>
            <table>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td>Then Want to select this one</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

我在变量中有页面内容,我只需要使用find方法。

2 个答案:

答案 0 :(得分:0)

  1. 使用:nth-​​child(),其索引以1
  2. 开头
  3. 另一个可能的选择是使用eq(),其索引以0
  4. 开头

    &#13;
    &#13;
    $("table").find("tr:nth-child(2)").find("tr:nth-child(2)").css("color","red")
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
            <tr></tr>
            <tr>
                First Want to select this tr
                <td>
                    <table>`
                        <tr>
                            <td></td>
                        </tr>
                        <tr>
                            <td>Then Want to select this one</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

第二行的伪选择器可用于获取其内容。

$.find('table tr:nth-child(2)').html()