有哪位知道offsetParent属性到底是如何定义的吗
例如<div id="d1"><div id="d2"></div></div>
d2的offsetParent是谁呢?

解决方案 »

  1.   

    <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>
      

  2.   

    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.
      

  3.   

    <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>&nbsp;</TD>
        <TD width=570><SPAN class=STYLE1>更改密码</SPAN></TD>
        <TD width=10>&nbsp;</TD></TR>
      <TR>
        <TD height=200>&nbsp;</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>&nbsp;</TD></TR>
      <TR>
        <TD height=40></TD>
        <TD align=middle><IMG id=chgPwdCofirmBtn 
          src="login_do.files/confirm.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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没有标准,这点比较让人郁闷
      

  4.   

    如果某个元素的父级或此元素层次结构中的其他元素使用相对或绝对定位,则 OffsetParent 将成为当前元素嵌套到的第一个相对或绝对定位元素。如果当前元素以上的任何元素都不是绝对或相对定位的,则 OffsetParent 将是文档的 BODY 标记。但IE和FF有不同.
      

  5.   

    但是通过IE测试的结果来说,好像与你说的不一样哦
    <div id="div1"><div id="div2"></div></div>
    div2的offsetParent竟然是div1
      

  6.   

    应该是BODY吧...如果这个直接放在BODY下..
      

  7.   

    就是直接放在body下,结果却是div2,这点让我在实现offsetParent的时候无所适从