function getAbsPoint(e)
{
  var x = e.offsetLeft, y = e.offsetTop;
  while(e=e.offsetParent){x += e.offsetLeft; y += e.offsetTop;}
  return {"x": x, "y": y};
}<td onclick="var xy=getAbsPoint(this); alert('x='+ xy.x +' y='+ xy.y)">

解决方案 »

  1.   

    to  meizz(梅花雪):
    必须要加上上级元素的相对坐标吗?我试了,这样算出来的好像还是有点差距,差几个像素,有没有一个办法能直接获得向鼠标一样的绝对坐标呢?
      

  2.   

    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style>
    div{text-align:center;}
    </style>
    </head><body style="margin:10;border:20px;" onclick="alert('body offsetTop:'+this.offsetTop)" bgcolor="eeeeee">
    <div onclick="alert(this.offsetTop);alert(this.offsetParent.tagName)" style="background-color:red;border:2px;">click here to test for body</div>
    一个对象相当于一个box,它包括以下几方面的空间:<br>
    <div style="background-color:cccccc;width:200;"> margin
      <div style="background-color:red;width:160;"> border
        <div style="background-color:blue;width:120;padding:10;margin:10;" onclick="alert(this.offsetTop);alert('offsetHeight:'+this.offsetHeight)"> padding
          <div style="background-color:white;" onclick="alert(this.offsetTop);"> 对象本身
          </div>
        </div>
      </div>
    </div>
    <br>
    以下,自己的初步总结,有待确认<br>
    offetTop是自己的border top 相对于offsetParent border top的距离.<br>
    &nbsp;&nbsp;(当offsetParent为body是,情况有点特殊,可能是body对象对border不大灵光所致).<br>
    offsetHeight是整border的占的height,不包括margin的范围<br>
    </body></html>
      

  3.   

    <script language=javascript>
    function getAbsPoint(obj)
    {
    var x,y;
    oRect = obj.getBoundingClientRect();
    x=oRect.left
    y=oRect.top
    alert("("+x+","+y+")")
    }</script>
    <table border=1><tr>
    <td onclick="getAbsPoint(this)">rrrere</td>
    </tr></table>
      

  4.   

    整理一下JS中获得窗口属性的方法1。获得屏幕的分辨率:
    screen.width
    screen.height2。获得窗口大小:
    document.body.clientWidth
    document.body.clientHeight3。获得窗口大小(包含Border、Scroll等元素)
    document.body.offsetWidth
    document.body.offsetHeight
    div相关的一些元素详细说明可以参考这张图
      

  5.   

    http://www.an-ping.com/forum/blog/detail.asp?blog_id=6&content_id=87
      

  6.   

    谢谢大家,我查了一下,好像event.x和event.clientX是一样的,都是相对于窗口的坐标,页面一滚动就出错了。有什么办法能获取鼠标在页面中的坐标呢??不要随着窗口的滚动而改变。
      

  7.   

    你可以这样想啊:首先知道 TD 的像素范围,如果鼠标的定位在这个范围之内,那么就是拖到这个td里了阿
    ====CSDN 小助手 V2.5 2005年11月05日发布====
    CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
    界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
    下载:http://szlawbook.com/csdnv2
      

  8.   

    event.x+document.body.scrollLeft
    event.y+document.body.scrollTop