this.getBoundingClientRect().top
this.getBoundingClientRect().left
左上角坐标是0,0

解决方案 »

  1.   

    不要用style,用currentStyle
    下面是得到对象的绝对位置
    <script language="javascript">
    function getIE(e){
    var t=e.offsetTop;
    var l=e.offsetLeft;
    while(e=e.offsetParent){
    t+=e.offsetTop;
    l+=e.offsetLeft;
    }
    alert("top="+t+"\nleft="+l);
    }
    </script>
      

  2.   

    //使用方法
    var tempobj = document.getElementById("控件ID");
    var pos = getAbsolutePosition(tempobj);
    alert(""+pos.x+","+pos.y);//函数
    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
    }
    return position;
    }
      

  3.   

    document.body.scrollTop+document.documentElement.scrollTop+tempobj.offsetTop + tempobj.clientTop