HTML元素的坐标

5 html javascript jquery html-table coordinates

我将创建一个选择'套索',用户可以使用它来选择表格的一部分.我认为在区域上定位div比尝试操作单元格边界要容易得多.

如果您不明白我的意思,请打开电子表格并拖动一个区域.我希望div与单元格边框完美对齐.

我非常清楚如何做到这一点,但我如何获得表格单元格(td)的(x,y)坐标(屏幕位置)?

gil*_*ly3 7

使用.offset()沿.height().width()如果需要的话.

var td = $(someTDReference);
var pos = td.offset();
pos.bottom = pos.top + td.height();
pos.right = pos.left + td.width();
// pos now contains top, left, bottom, and right in pixels
Run Code Online (Sandbox Code Playgroud)

编辑:.position(),使用.offset().上面更新了.

编辑:已更改pos.width()td.width()