获取知道位置的表单元格和行

Get table cell and row knowing the position

本文关键字:单元格 表单 位置 获取      更新时间:2023-09-26

我用它来访问特定于表的单元格

$('table').find('td:eq(' + pos + ')');

对于一个3x3的表,比如:

<table>
 <tr>
  <td></td>
  <td></td>
  <td></td>
 </tr>
 <tr>
  <td></td>
  <td></td>
  <td></td>
 </tr>
 <tr>
  <td></td>
  <td></td>
  <td></td>
 </tr>
</table>

的位置等于:

012
345
678

如何让单元格的位置知道它的位置,比如知道1,得到[0][1] ?

eq()使用索引。索引对td-tr是这样工作的。

tr  td
0   0 1 2
1   0 1 2
2   0 1 2

因此,考虑[0][1]

你需要$('tr:eq(0) td:eq(1)')

坐标:

var trIndex = Math.ceil(pos / trCount)
var tdIndex = pos % (tdCount/trCount)
$('tr:eq('+(trIndex-1)+') td:eq('+(tdIndex-1)+')')

如果您只想从数字中获得类似于字符串的输出,请使用%

if (number%cell_count == 0){
    y = number/cell_count -1;
    x = cell_count-1;
} else{
    x = number%cell_count - 1;
    y = Math.floor((number - x )/cell_count);
}

然后是$('tr:eq(y) td:eq(x)')

这里是提琴