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
jQuery - select the second row in a table?
提问by Mustapha George
How do I target the second <tr>
in a table using jQuery? Do I use .closest
or nth-child
?
如何<tr>
使用 jQuery定位表中的第二个?我使用.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>
回答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 :first
selector in combination with the insertAfter()
function:
将:first
选择器与insertAfter()
函数结合使用:
$("TheElementToInsert").insertAfter(".myClass tr:first");