【问题标题】:Jquery select statementjQuery 选择语句
【发布时间】:2013-06-22 09:28:36
【问题描述】:

如果我有这样的 html 结构

<table>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<table>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<table>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
etc...

表格标签重复的地方。我如何编写一个 select 语句,从每个表标记中获取第三个 td 标记?

谢谢

【问题讨论】:

    标签: javascript jquery select jquery-selectors


    【解决方案1】:

    你可以这样做:

    $('table tr td:nth-child(3)')
    

    这是fiddle demo

    【讨论】:

    • $('table &gt; tbody &gt; tr &gt; td:nth-child(3)') 呢?
    • 当然可以 - &gt; 是一个更具体的情况,它专门寻找子节点(而不是大子节点)。如果这是所有表的结构一致,那么你的情况就可以了。
    【解决方案2】:

    只需使用nth-child selector

    $('td:nth-child(3)')
    

    【讨论】:

      【解决方案3】:

      试试这个:$("td:nth-child(2)").append(" - 2nd!");

      【讨论】:

        【解决方案4】:

        你可以实际使用:

        $('table tbody tr td:last-child')
        

        这也适用于 css3:

        table tbody tr td:last-child{
            background:red;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-04-22
          • 2010-10-03
          • 2017-12-02
          • 2011-06-09
          • 2011-07-12
          • 2015-01-28
          • 2011-07-05
          • 2019-01-04
          • 2014-02-03
          相关资源
          最近更新 更多