使用jQuery通过索引获取td

34 html javascript indexing jquery html-table

我知道如何使用jQuery获取单元格的行和列索引,但我无法弄清楚相反的情况.给定行和列索引,我将如何访问此位置的td?

Fel*_*ing 80

使用纯JavaScript:

// table is a reference to your table
table.rows[rowIndex].cells[columnIndex]
Run Code Online (Sandbox Code Playgroud)

参考: HTMLTableElement,HTMLTableRowElement


使用jQuery,您可以使用.eq():

$('#table tr').eq(rowIndex).find('td').eq(columnIndex)
// or
$('#table tr:eq(' + rowIndex + ') td:eq(' + columnIndex + ')')
Run Code Online (Sandbox Code Playgroud)

  • 怎么样 - 简单的属性访问,看不到单个函数调用。:-) (2认同)

Ric*_*ett 9

如何使用nth-child选择器?

http://api.jquery.com/nth-child-selector/

var row = 4;
var col = 2

var cell = $('table#tableId tr:nth-child(' + row + ') td:nth-child(' + col + ')');
Run Code Online (Sandbox Code Playgroud)

请注意,子索引是基于1的,而不是通常的基于0的索引.