Javascript 如何使用jQuery获取指定索引处的表格单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3052571/
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-23 03:06:51  来源:igfitidea点击:

How to get table cell at specified index using jQuery

javascriptjquery

提问by jwaliszko

I know that I can get first or last table cell (e.g. for last row) using jQuery expression like below:

我知道我可以使用如下 jQuery 表达式获取第一个或最后一个表格单元格(例如最后一行):

first cell: $('#table tr:last td:first')or last cell: $('#table tr:last td:last')

第一个单元格:$('#table tr:last td:first')或最后一个单元格:$('#table tr:last td:last')

But, how can I get cell at specific index, for example index 2, using similar expression, i.e. something like $('#table tr:last td:[2]')?

但是,如何使用类似的表达式(即类似的表达式)获取特定索引处的单元格,例如索引 2 $('#table tr:last td:[2]')

Regards.

问候。

回答by Philippe Leybaert

Yes:

是的:

$('#table tr:last td:eq(1)')

that will give you the second tdin the last row.

这会给你td最后一行的第二个。

回答by Tim Down

It's very simple without jQuery, and will work faster and in more browsers:

没有 jQuery,它非常简单,并且可以更快地在更多浏览器中运行:

var table = document.getElementById("table");
var row = table.rows[table.rows.length - 1];
var cell = row.cells[2];

回答by Blair Conrad

Use the nth-child selector.

使用第n 个子选择器

For example,

例如,

$('#table tr:last td:nth-child(2)')