<DIV onclick="aa(this)"></DIV><script language="javascript">
function aa(obj)
{
    var pos = getAbsolutePosition(obj);
    alert((pos.x - parsePx(obj.style.left)));
    alert((pos.y - parsePx(obj.style.top)));
}function parsePx(inputstr)
{
    var tempstr = inputstr.replace("px","");
    var tempint = parseInt(tempstr);
    return isNaN(tempint)?0:tempint;
}function getAbsolutePosition(obj)
{
position = new Object();
position.x = 0;
position.y = 0;
var tempobj = obj;
while(tempobj!=null && tempobj!=document.body)
{
position.x += tempobj.offsetLeft + tempobj.clientLeft;
position.y += tempobj.offsetTop + tempobj.clientTop;
tempobj = tempobj.offsetParent
}
position.x += document.body.scrollLeft;
position.y += document.body.scrollTop;
return position;
}
</script>

解决方案 »

  1.   

    虽然很长,但不对,我把DIV设置成absolute时,总是显示同样的值.
      

  2.   

    哦 不好意思.......... 我理解错误<DIV onclick="aa(this)"></DIV><script language="javascript">
    function aa(obj)
    {
        alert((window.event.x - parsePx(obj.style.left)));
        alert((window.event.y - parsePx(obj.style.top)));
    }function parsePx(inputstr)
    {
        var tempstr = inputstr.replace("px","");
        var tempint = parseInt(tempstr);
        return isNaN(tempint)?0:tempint;
    }
      

  3.   

    ie用offsetX和offsety,firefox用layerX和layerY
      

  4.   

    上面打错一个是offsetY
    你写的时候可以这样写就兼容了ie和firefox
    var _offsetX=offsetX || layerX
    var _offsetY=offsetY || layerY