this.offsetParent返回包含这个元素的元素,offsetTop和offsetLeft就是相对于offsetParent定义的,看看下面的代码你就明白了:
<HTML>
<HEAD>
<TITLE>Elements: Positions</TITLE>
<SCRIPT LANGUAGE="JScript">
function showPosition()
{
var oElement = document.all.oCell;
alert("The TD element is at (" + oElement.offsetLeft +
"," + oElement.offsetTop + ")\n" + "The offset parent is "
+ oElement.offsetParent.tagName );
}
</SCRIPT>
</HEAD>
<BODY onload="showPosition()">
<P>This document contains a right-aligned table.
<TABLE BORDER=3 ALIGN=right>
<TR>
<TD ID=oCell>This is a small table.</TD>
</TR>
</TABLE>
</BODY>
</HTML>
你可以把table 的border设得大点或小点试试
offsetParent --Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object.
有一些具体的细节:
1.for ie5.0 and above,the offsetParent property return the table object for the td object in ie4.0 it returns the tr object
2.大多数情况下offsetParent得到的是body.div ,span具有容器这种特性
3.offsetParent与position有很大的关系