jQuery 按索引获取行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6428658/
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
get row by index
提问by clarkk
how can you get a row by the index?
你怎么能通过索引获得一行?
var rows = $('tr', tbl);
rows.index(0).addClass('my_class');
回答by Matt Ball
Use .eq()
.
使用.eq()
.
var rows = $('tr', tbl);
rows.eq(0).addClass('my_class');
...or for your simple case, .first()
:
...或者对于您的简单情况,.first()
:
rows.first().addClass('my_class');
回答by Town
回答by Blindy
var row=$('tr:eq(5)', tbl); // returns the 5th row
回答by Amin Eshaq
you can do
你可以做
$('tr:eq(0)', tbl).addClass('my_class');
more on this http://api.jquery.com/eq-selector/
回答by user113716
You could use the native rows
[docs]property on the HTMLTableElement
.
你可以使用本机rows
[文件]的财产HTMLTableElement
。
$(tbl[0].rows[0]).addClass('my_class');
As noted by @Felix, I've assumed that tbl
is a jQuery object. If not, do this:
正如@Felix所指出的,我假设那tbl
是一个 jQuery 对象。如果没有,请执行以下操作:
$(tbl.rows[0]).addClass('my_class');
回答by user113716
You can use nth-child in your selector:
您可以在选择器中使用 nth-child:
$('tr td:nth-child(3)').addClass('my_class');
Will get the third td.
将获得第三个 td。
回答by Chandu
回答by Piskvor left the building
http://api.jquery.com/get/says:
Retrieve the DOM elements matched by the jQuery object.
.get( [index] )
index A zero-based integer indicating which element to retrieve.
检索与 jQuery 对象匹配的 DOM 元素。
.get( [index] )
index 一个从零开始的整数,指示要检索的元素。
Note that you'll get the DOM object, not a jQuery one:
请注意,您将获得 DOM 对象,而不是 jQuery 对象:
var rows = $('tr', tbl);
$(rows.get(0)).addClass('my_class');