如何使用jQuery获取表格中的单元格()x和y坐标?
我正在寻找一种在表格中获取单元格XY位置的好方法。 (不要把它与css-position混淆,我在笛卡尔坐标系中寻找X和Y坐标)。
我们知道,我们可以使用ex: $('#grid')[0].rows[5].cells[7]
在表格中获取特定单元$('#grid')[0].rows[5].cells[7]
。
但是,如果我想在单击特定单元格时获得值5x7
,即:
$('#grid td').click(function(){ alert("My position in table is: " + myPosition.X + "x" + myPosition.Y); });
我想,最简单的方法是在后端创建表时将innerHTML,ID或CSS类添加到每个单元格(
DOM Level 2 HTML cellIndex :
alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);
window.onload = function () { document.getElementsByTagName('table')[0].addEventListener('click', function(e) { alert("My position in table is: " + e.target.parentElement.rowIndex + "x " + e.target.cellIndex + "y"); }, false); }
tr, td { padding: 0.6rem; border: 1px solid black } table:hover { cursor: pointer; }
0, 0 0, 1 0, 2 1, 0 1, 1 1, 2