如何使用jQuery在表中获取单元格(< td>)x和y坐标? [英] How to get cells (<td>) x and y coordinate in table using jQuery?

查看:63
本文介绍了如何使用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);

这篇关于如何使用jQuery在表中获取单元格(&lt; td&gt;)x和y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆