jQuery 按行号和列号选择表格中的任意单元格

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

Selecting an arbitrary cell in a table by row and column number

jqueryhtml-table

提问by Acorn

I have a large table, and I need to be able to select a specific cell using it's cell/row coordinates.

我有一个大表,我需要能够使用它的单元格/行坐标选择一个特定的单元格。

What's the most elegant way of doing this using jQuery?

使用 jQuery 执行此操作的最优雅方法是什么?

回答by Andrew Whitaker

This is one case where I think using native JavaScript actually makes the code easier to understand:

这是我认为使用原生 JavaScript 实际上使代码更易于理解的一种情况:

var table = $("#table")[0];
var cell = table.rows[1].cells[1]; // This is a DOM "TD" element
var $cell = $(cell); // Now it's a jQuery object.

Note that just selecting the tableelement will make rowsinclude those rows in your thead(and tfoot). What you probably want is:

请注意,只需选择该table元素,就会rows在您的thead(和tfoot)中包含这些行。你可能想要的是:

var table = $("#table tbody")[0];
/* remaining code from above */

Here's an example: http://jsfiddle.net/CgqQt/

这是一个例子:http: //jsfiddle.net/CgqQt/

回答by mg1075

After reviewing the fiddle you posted in one of your comments, this could also work.

在查看您在其中一条评论中发布的小提琴后,这也可以奏效。

http://jsfiddle.net/CGrP9/6/

http://jsfiddle.net/CGrP9/6/

$('tbody tr').eq(2).find('td').eq(2).css('background-color', 'green');

回答by Sathvik reddy Gaddam

$($("table#wall_layout tr")[row]).find("td")[col]

$($("table#wall_layout tr")[row]).find("td")[col]

回答by Blender

I'm pretty sure this selects the cell at coordinate (9, 9). Let me test:

我很确定这会选择坐标处的单元格(9, 9)。让我测试一下:

$('table tr:eq(10) > td:eq(10)')