This 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()">
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>