muxrwc(需时越兔)和其他朋友帮帮忙,谢谢
<HTML>
<HEAD>
<TITLE> Elements: Positions </TITLE>
<SCRIPT LANGUAGE= "JScript ">
function showPosition()
{
var oElement = document.all.oCell;
aa=document.getElementById( "parentdiv ");
bb=document.getElementById( "sondiv ");
alert( "The TD element is at ( " + aa.offsetTop +
", " + aa.offsetLeft + ")\n " + "The offset parent is "
+ aa.offsetParent.tagName);
}
</SCRIPT>
</HEAD>
<BODY onload= "showPosition() ">
<TABLE ALIGN=right border= "100px " cellspacing= "0 " cellpadding= "0 " style= "position:relative; ">
<TR cellspacing= "0 " cellpadding= "0 ">
<TD ID= "oCell " > <div id= "parentdiv " style= "position:relative; "> parentdiv <div id= "sondiv " style= "position:relative "> sondiv </div> </div> </TD>
</TR>
</TABLE>
</BODY>
</HTML>
为什么 parentdiv的offsetwidth是200也就是表格边框的2倍,以上程序可以直接使用,谢谢,研究一下午没有测试清楚offsetwidth就是它父层间的距离
------解决方案--------------------关注~~~似乎因该去css那问一下
------解决方案--------------------我测试全是100啊,没问题啊。。
如果想获取实际距离
把offsetParent.offset...
加上就可以了。。
------解决方案--------------------var offset = function (o) {
//设置定位
var $x = $y = 0;
do { $x += o.offsetLeft, $y += o.offsetTop; }
while (o = o.offsetParent);
return { x : $x, y : $y };
}