如何使用jQuery在表中获取单元格(< td>)x和y坐标?
我正在寻找一种在表格中获取X-Y位置的好方法。 (不要把它与css-position混淆,我在笛卡尔坐标系中寻找X和Y坐标)。
I'm looking for a good way to get cells X-Y position in a table. (do not confuse it with css-position, I am looking for X and Y coordinate in Cartesian coordinate system).
我们知道,我们可以得到一个特定的单元格表使用ex: $('#grid')[0] .rows [5] .cells [7]
。
As we know, we can get particular cell in a table using ex: $('#grid')[0].rows[5].cells[7]
.
但是,当我点击特定单元格时,如果我想得到 5x7
的值,即:
But, what if I want to get the value 5x7
when I click on particular cell, i.e:
$('#grid td').click(function(){
alert("My position in table is: " + myPosition.X + "x" + myPosition.Y);
});
我想,最简单的方法是将innerHTML,ID或CSS类添加到每个单元格(< td class =p-5x7>
等)在后端创建表格,然后解析它,然后点击返回,但是有什么办法可以获得这些纯粹基于DOM的坐标?
I guess, the easiest way is to add innerHTML, ID or CSS class to every cell (<td class="p-5x7">
etc.) when creating table in back end, then parse it, and return on click, but is there any way to get these coordinates based purely on DOM?
DOM Level 2 HTML cellIndex :
DOM Level 2 HTML cellIndex:
alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);