如何使用jQuery获取表格中的单元格(<td>)x和y坐标?

roc*_*hal 13 javascript jquery coordinates

我正在寻找一种在表格中获取单元格XY位置的好方法.(不要把它与css-position混淆,我在笛卡尔坐标系中寻找X和Y坐标).

我们知道,我们可以使用ex:在表中获取特定单元格$('#grid')[0].rows[5].cells[7].

但是,如果我想5x7在点击特定单元格时得到该值,即:

$('#grid td').click(function(){
   alert("My position in table is: " + myPosition.X + "x" + myPosition.Y);
});
Run Code Online (Sandbox Code Playgroud)

我想,最简单的方法是<td class="p-5x7">在后端创建表时向每个单元格(等)添加innerHTML,ID或CSS类,然后解析它,然后单击返回,但有没有办法完全基于这些坐标获取这些坐标DOM?

bob*_*nce 38

DOM Level 2 HTML cellIndex:

alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);
Run Code Online (Sandbox Code Playgroud)

  • 嗯...是的...我删除了我的答案,因为这个答案比我的好得多.:-) 1+ (5认同)