按行和列号选择表中的任意单元格
问题描述:
我有一张大桌子,我需要能够使用它的单元格/行坐标来选择特定的单元格。
I have a large table, and I need to be able to select a specific cell using it's cell/row coordinates.
这样做最优雅的方法是什么使用jQuery?
What's the most elegant way of doing this using jQuery?
答
这是我认为使用本机JavaScript实际上使代码更容易理解的一种情况:
This is one case where I think using native JavaScript actually makes the code easier to understand:
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.
请注意,只需选择表
元素make rows
在 thead
中包含这些行(以及 tfoot
)。你可能想要的是:
Note that just selecting the table
element will make rows
include those rows in your thead
(and tfoot
). What you probably want is:
var table = $("#table tbody")[0];
/* remaining code from above */
以下是一个例子:http://jsfiddle.net/CgqQt/