【问题标题】:jQuery - select the second row in a table?jQuery - 选择表格中的第二行?
【发布时间】:2011-12-28 06:44:37
【问题描述】:

如何使用 jQuery 定位表中的第二个 <tr>?我使用.closest 还是nth-child

// something like this..
var MyLocation = $('.myclass').closest('tr').after('tr');   // fixed


<table class ='myclass'>
<tr>
  <td></td>
</tr>
   <!-- put some thing here -->
<tr>

</tr>

【问题讨论】:

  • 还要确保在类名之前使用 $('.myclass') 而不是 $('myclass')。如果没有句点,您将寻找一个名为 的元素

标签: jquery jquery-selectors


【解决方案1】:
$('.myclass tr').eq(1)

这将抓取第二个。

【讨论】:

【解决方案2】:

使用第 n 个子选择器。见http://api.jquery.com/nth-child-selector/

$('.myclass tr:nth-child(2)')

【讨论】:

    【解决方案3】:

    :first 选择器与insertAfter() 函数结合使用:

    $("TheElementToInsert").insertAfter(".myClass tr:first");
    

    【讨论】:

    • 我认为他不想添加 。听起来他更像是试图到达现有的第二个 .
    • @Zak,基于 OP 中第一个 tr 之后的 &lt;!-- put some thing here --&gt;,我不同意。
    • 哦,对不起!我没有足够注意那在哪里。我认为它在 和 标签之间。我的错!
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签