求:js 返回dom对象的css的left和top值的代码?
jquery有这个方法,但不值纯js是如何得到的。试了一下,不行。

解决方案 »

  1.   

    var top = document.getElementById('id').offsetTop+document.body.scrollTop
    var left = document.getElementById('id').offsetLeft;
      

  2.   


      <style>
    .inp {position:absolute; left:50px; top:100px}
      </style>
      <INPUT TYPE="text" NAME="inp_1" id="inp_1" value="abc" class="inp">
      <INPUT TYPE="button" VALUE="Click" ONCLICK="fun()">
    <SCRIPT LANGUAGE="JavaScript">
    function fun(){
    var inp1 = document.getElementById("inp_1");
    alert("left:"+inp1.currentStyle.left+"; top:"+inp1.currentStyle.top);
    }
    </SCRIPT>
      

  3.   

    JQuery$("#id").css("left");$("#id").css("top");不行?
      

  4.   

    var top =parseInt( document.getElementById('id').style.top);
    var left =parseInt( document.getElementById('id').style.left);
      

  5.   

    可以参考getboundingclientrect()方法
      

  6.   

    var top =parseInt( document.getElementById('id').style.top);
    var left =parseInt( document.getElementById('id').style.left);
      

  7.   

    var getPos:function(o){//取元素坐标
        var x = 0, y = 0;
        do{
            x += o.offsetLeft;
            y += o.offsetTop;
        }while(o=o.offsetParent);
        return {'x':x,'y':y};
    }
      

  8.   

    var top = parseInt(document.getElementById('id').offsetTop)+parseInt(document.body.scrollTop);
    var left = parseInt(document.getElementById('id').offsetLeft);