JS获取table中td的绝对坐标

<html>

<script language="javascript">
function findPosX(obj) {
var curleft = 0;

if (obj.offsetParent) { //返回父类元素,大多说offsetParent返回body
   while (obj.offsetParent) {//遍历所有父类元素
    curleft += obj.offsetLeft;//当前元素的左边距
    obj = obj.offsetParent;        
   }
} else if (obj.x) curleft += obj.x;
return curleft;
}

function findPosY(obj) {
var curtop = 0;

if (obj.offsetParent) {
   while (obj.offsetParent) {

    curtop += obj.offsetTop;
    obj = obj.offsetParent;
   }
} else if (obj.y) curtop += obj.y;
return curtop;
}
function getPos(){
return {x:findPosX(document.getElementById("p36table")),y:findPosY(document.getElementById("p36table"))};
}

function showPos(){
var pos = getPos();
alert("x=="+pos.x+"y=="+pos.y);

}
</script>

<div id="parentdiv" style="position:relative; border:5px solid;" >
         <table id="p36table" style="position: relative; " width="185" border="1" align="center" cellpadding="0" cellspacing="0">
   <tr>
   <td>
          <iframe frameborder=0 scrolling=no width="185" height="170" marginHeight=0 marginWidth=0 align="left" 
      src='#'></iframe>
   </td>
   <td onClick="javascript:showPos();">测试获得坐标</td>
   </tr>
      </table>
   
   </div>   
</html>







附注:

方法1:

function   getAbsPoint(e)   
{   
    var   x   =   e.offsetLeft,   y   =   e.offsetTop;   
    while(e=e.offsetParent) 
    { 
       x   +=   e.offsetLeft;   
       y   +=   e.offsetTop;
    } 
    alert("x:"+x+","+"y:"+y);   
}

方法2:

function   getAbsPoint(obj)   
{   
   var   x,y;   
   oRect   =   obj.getBoundingClientRect();   
x=oRect.left   
y=oRect.top   
alert("("+x+","+y+")")   
}

JS中获得窗口属性的方法 

1。获得屏幕的分辨率: 
screen.width 
screen.height 

2。获得窗口大小: 
document.body.clientWidth 
document.body.clientHeight 

3。获得窗口大小(包含Border、Scroll等元素) 
document.body.offsetWidth 
document.body.offsetHeight 

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值