jQuery - 选择表格中的第二行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8115739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:02:13  来源:igfitidea点击:

jQuery - select the second row in a table?

jqueryjquery-selectors

提问by Mustapha George

How do I target the second <tr>in a table using jQuery? Do I use .closestor nth-child?

如何<tr>使用 jQuery定位表中的第二个?我使用.closestnth-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>

回答by Seth

$('.myclass tr').eq(1)

This will grab the second one.

这将抓住第二个。

回答by Rob Cowie

Use the nth-child selector. See http://api.jquery.com/nth-child-selector/

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

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

回答by James Hill

Use the :firstselector in combination with the insertAfter()function:

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

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