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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:51:40  来源:igfitidea点击:

get row by index

jquery

提问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

Using either the eq()function:

使用以下任一eq()功能:

rows.eq(0).addClass('my_class');


Or the :eq()selector:


或者:eq()选择器:

$('tr:eq(0)', tbl).addClass('my_class');

回答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/

更多关于这个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 tblis 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 DanielB

Use eq()

使用eq()

$('tr', tbl).eq(0).addClass('my_class');

回答by Chandu

For the first element (index 0) the answer provided to your earlier question should be fine.

对于第一个元素(索引 0),为您之前的问题提供的答案应该没问题。

For any nth element use eqselector

对于任何第 n 个元素使用eq选择器

e.g:

例如:

var rows = $('tr:eq(8)', tbl);

回答by Piskvor left the building

http://api.jquery.com/get/says:

http://api.jquery.com/get/说:

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');