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

时间:2022-01-02 22:15:50

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执行此操作的最优雅方法是什么?

3 个解决方案

#1


21  

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 table element will make rows include those rows in your thead (and tfoot). What you probably want is:

请注意,只选择table元素将使行包含thead(和tfoot)中的行。你可能想要的是:

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

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

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

#2


4  

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

在审阅了您在其中一条评论中发布的小提琴后,这也可以起作用。

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

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

#3


0  

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

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

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

#1


21  

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 table element will make rows include those rows in your thead (and tfoot). What you probably want is:

请注意,只选择table元素将使行包含thead(和tfoot)中的行。你可能想要的是:

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

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

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

#2


4  

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

在审阅了您在其中一条评论中发布的小提琴后,这也可以起作用。

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

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

#3


0  

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

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

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