如何获得精确的鼠标坐标?为什么我的代码不起作用?

Lee*_*fin 3 javascript jquery jquery-selectors dom-events

我想在鼠标下方显示一些东西,我试着用:

$('#my-graph').mousedown(function(evt){

      // show object at:
      var x= evt.screenX, y=evt.screenY;

      //or show object at:
     var x2=evt.pageX, y2=evt.pageY;

     //code to render object at (x,y) and (x2,y2)
     ......  
});
Run Code Online (Sandbox Code Playgroud)

但是上面的(x,y)和(x2,y2)都没有将渲染的对象放在鼠标点击的位置,并且显示对象距离鼠标的位置有一段距离,为什么呢?

我使用位置属性渲染对象,位置相对于#my-graph div,div的大部分角落应该是原点(0,0)

pim*_*vdb 5

你似乎想要offsetXoffsetY:http://jsfiddle.net/f52Gg/.

$("div").mousedown(function(e){
    alert(e.offsetX + " " + e.offsetY);
});
Run Code Online (Sandbox Code Playgroud)