<div style = "width:300px;margin:0px auto;">
      <div style = "float:left;width:40%">
             账号:
      </div>
      <div style = "float:left;width:60%">
             <input type = "text" name = "username" />
      </div>
      <div style = "float:left;width:40%">
             密码:
      </div>
      <div style = "float:left;width:60%">
             <input type = "password" name = "password" />
      </div>
</div>
这样的一个代码,就是div做成表格的样子,里面有个input元素,现在我想取得input的offersetTop 和offersetLeft这样的属性,但是,得到的结果不正确,  top 是0 or 1;
怎么获得它的真实的x  与 y 点呢。 。     我是想做一个用div做提示在这个input旁边显示出来,所以先获得它的  x  和y 坐标

解决方案 »

  1.   

    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};
    }
      

  2.   

    <div style = "positon:absolute;width:300px;margin:0px auto;z-index:1;">这样就可以用offersetTop和offersetLeft
      

  3.   

    再次谢谢。 。     代码没问题, 其中的核心部分也能看懂。 。 问题解决了。 。  可是我不太懂 var getPos:function(o){// 
    以及 return {'x':x,'y':y};这俩句话的意思。。     return  内容,貌似是json 形式,  但 var getPos:function() ;  我不太懂 getPost:function 是什么意思, 怎么调用呢。 。   调用后,怎么取得返回值呢  我是js 菜鸟。。 。