<HTML> <HEAD> <TITLE>Elements: Positions</TITLE> <SCRIPT LANGUAGE="JScript"> function showPosition() { var oElement = document.all.d2; alert("The TD element is at (" + oElement.offsetLeft + "," + oElement.offsetTop + ")\n" + "The offset parent is " + oElement.offsetParent.tagName ); } </SCRIPT> </HEAD> <BODY onload="showPosition()"> <div id="d1"> <div id="d2"> </div> </div> </BODY> </HTML>
Most 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 Internet Explorer 4.0 it returns the tr object. You can use the parentElement property to retrieve the immediate container of the table cell.
<HEAD>
<TITLE>Elements: Positions</TITLE>
<SCRIPT LANGUAGE="JScript">
function showPosition()
{
var oElement = document.all.d2;
alert("The TD element is at (" + oElement.offsetLeft +
"," + oElement.offsetTop + ")\n" + "The offset parent is "
+ oElement.offsetParent.tagName );
}
</SCRIPT>
</HEAD>
<BODY onload="showPosition()">
<div id="d1"> <div id="d2"> </div> </div>
</BODY>
</HTML>
<head>
<LINK
href="http://10.10.30.5/temp/accountservice.css" type=text/css rel=stylesheet>
<STYLE>BODY {
MARGIN: 0px
}
</STYLE>
<script>
function getoffsetparent()
{
var obj = document.getElementById("oldPwdDiv");
while(obj){
alert(obj.tagName);
obj = obj.offsetParent;
}
}
</script>
</head>
<body>
<div id="div1"><div id="div2">test</div></div>
<input type="button" onclick="getoffsetparent();" value="get">
<DIV id=cg_pwd_div
style="BACKGROUND-IMAGE: url(http://192.166.162.237:7001/Pay-Gateway-Web/looksfeel/accountservice/themes/blue/images/zf_div_bg.gif)">
<TABLE cellSpacing=0 cellPadding=0 width=590 border=0>
<TBODY>
<TR>
<TD width=10 height=40> </TD>
<TD width=570><SPAN class=STYLE1>更改密码</SPAN></TD>
<TD width=10> </TD></TR>
<TR>
<TD height=200> </TD>
<TD align=middle>
<TABLE cellSpacing=0 cellPadding=0 width=350 border=0>
<TBODY>
<TR>
<TD width=134 height=50>
<DIV class=STYLE6 id=oldPwd align=right>原始密码:</DIV></TD>
<TD align=left width=216>
<DIV id=oldPwdDiv></DIV></TD></TR>
<TR>
<TD height=50>
<DIV class=STYLE6 id=newPwd align=right>新密码:</DIV></TD>
<TD align=left>
<DIV id=newPwdDiv></DIV></TD></TR>
<TR>
<TD height=50>
<DIV class=STYLE6 id=newPwd2 align=right>确认新密码:</DIV></TD>
<TD align=left>
<DIV id=newPwd2Div></DIV></TD></TR></TBODY></TABLE></TD>
<TD> </TD></TR>
<TR>
<TD height=40></TD>
<TD align=middle><IMG id=chgPwdCofirmBtn
src="login_do.files/confirm.gif"> <IMG
id=chgPwdCancelBtn src="login_do.files/back2.gif"></TD>
<TD></TD></TR>
<TR>
<TD height=40></TD>
<TD align=right>
<DIV class=STYLE1 id=chgpwd_tip
style="WIDTH: 530px; LINE-HEIGHT: 36px; HEIGHT: 36px; TEXT-ALIGN: left">请输入6位旧的账户密码</DIV></TD>
<TD></TD></TR></TBODY></TABLE><INPUT id=oldPwdHid type=hidden> <INPUT
id=newPwdHid type=hidden> <INPUT id=newPwd2Hid type=hidden></DIV>
</body>
</html>那上面的页面中,alert会显示那些tagName呢,我主要是想知道不运行js,光读就知道节点的offsetParent是谁?
好像offsetParent没有标准,这点比较让人郁闷
<div id="div1"><div id="div2"></div></div>
div2的offsetParent竟然是div1