offsetParent Property--------------------------------------------------------------------------------Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object.SyntaxHTML N/A 
Scripting [ oElement = ] object.offsetParent Possible ValuesoElement Object that receives the container object. The property is read-only. The property has no default value.Expressions can be used in place of the preceding value(s), as of Microsoft?Internet Explorer 5. For more information, see Dynamic Properties.ResMost of the time the offsetParent property returns the body object.Note  In Microsoft?Internet Explorer 5, the offsetParent property returns the TABLE object for the TD object; in Microsoft?Internet Explorer 4.0 it returns the TR object. You can use the parentElement property to retrieve the immediate container of the table cell.
ExampleThis example shows how to determine the position of a TD object. Although the TD object appears to the far right in the document, its position is close to the x-axis and y-axis, because its offset parent is a TABLE object rather than the document body. For Internet Explorer 4.0, this same example returns a position of 0,0 because the offset parent is the table row.<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=1 ALIGN=right>
  <TR>
    <TD ID=oCell>This is a small table.</TD>
  </TR>
</TABLE>
</BODY>
</HTML>

解决方案 »

  1.   

    parentElement Property--------------------------------------------------------------------------------Retrieves the parent object in the object hierarchy. SyntaxHTML N/A 
    Scripting [ oElement = ] object.parentElement Possible ValuesoElement Object that receives the parent. The property is read-only. The property has no default value.Expressions can be used in place of the preceding value(s), as of Microsoft?Internet Explorer 5. For more information, see Dynamic Properties.ResThe topmost object returns null as its parent. 
      

  2.   

    offsetParent可以理解为是对该元素的布局,也就是页面的位置起影响的上层元素。
    div如果不设置position属性,就不会对下层的元素的位置产生影响,事实也是这样。不设position的div仅仅是一个块流元素
    <table border="1">
    <tr id="t"><td>12321312</td></tr>
    </table>
    <div style="border: 1px solid black;position:absolute;">
    <form>
    <input type="checkbox" id="cc">
    </form>
    </div><SCRIPT LANGUAGE="JavaScript">
    alert(t.parentElement.tagName)
    alert(t.offsetParent.tagName)alert(document.all.cc.parentElement.tagName)
    alert(document.all.cc.offsetParent.tagName)
    </SCRIPT>