offsetLeft和offsetTop的替代?

时间:2011-06-13 13:44:25

标签: javascript jquery

我写了一个小画布应用程序并最终尝试将其合并到我的博客中,但现在我发现鼠标单击offsetLeft和offsetRight始终为0.

我真的不知道为什么,但我如何得到这些信息呢?

如果有人没有在这篇文章中看到标签:是的我正在使用jQuery进行鼠标事件。

$('#'+canvasId).mousedown(function(e){

    that.mouse.down = true;
    that.mouse.downx = e.pageX-this.offsetLeft;
    that.mouse.downy = e.pageY-this.offsetTop;

    that.mouse.dialogDown = k.operations.interface.getHudItem(that.mouse.downx, that.mouse.downy);

    k.operations.interface.mouseDown(that.mouse.downx, that.mouse.downy);
});

3 个答案:

答案 0 :(得分:0)

这有用吗?

var offset = jQuery(this).css('offset');
alert( 'Left: ' + offset.left + '\nTop: ' + offset.top );

答案 1 :(得分:0)

应该是offset.leftoffset.top,而不是offsetLeftoffsetTop

答案 2 :(得分:0)

试试这个

 $('#'+canvasId).mousedown(function(e){

     tt = $( this );

     tof = tt.offset()

     that.mouse.down = true;
     that.mouse.downx = e.pageX-tof.left;
     that.mouse.downy = e.pageY-tof.top;

     that.mouse.dialogDown = k.operations.interface.getHudItem(that.mouse.downx, that.mouse.downy);

k.operations.interface.mouseDown(that.mouse.downx, that.mouse.downy);
});