Blog Reader RSS LoveCherry 技术无极限 GEO MVP MS Project开源技术

如果父div的position定义为relative,子div的position定义为absolute,那么子div的style.left的值是相对于父div的值,
这同offsetLeft是相同的,区别在于:
1. style.left 返回的是字符串,如28px,offsetLeft返回的是数值28,如果需要对取得的值进行计算,
还用offsetLeft比较方便。
2. style.left是读写的,offsetLeft是只读的,所以要改变div的位置,只能修改style.left。
3. style.left的值需要事先定义,否则取到的值为空。而且必须要定义在html里,我做过试验,如果定义在
css里,style.left的值仍然 为空,这就是我刚开始碰到的问题,总是取不到style.left的值。

offsetLeft则仍然能够取到,无需事先定义div的位置。

// 这个函数是对 一个无穷分类的 下拉框的操作,页面开始只有一个下拉框,当选中下拉框一个值后,
动态生成一个select, select的项是子分类,同时要使子分类的select框后移 20px;

 

Code

 {      $('itemtree_parentid').value = selectObj.value;      

  var level = parseInt( selectObj.level ); // 当前选中的分类的下拉框是第几层      

var left = parseInt( selectObj.offsetLeft ); // 取得左位置值      

var td = $('itemtree_cats'); // 分类容器      

var objs = td.getElementsByTagName( 'SELECT' );      // 去掉当前选中下拉框的所有子分类的下拉框      

for( var c=0; c<objs.length; c++ )

 {        if( parseInt(objs[c].level) > level )  

        td.removeChild( objs[c] );        }    

  // 重新生成一下子下拉框     

 var new_select = document.createElement('SELECT');      

new_select.onchange = function() 

{ itemtree_cats_change(this); };      

new_select.level = level + 1;     

 new_select.style.position = 'relative';      

new_select.style.left = (left + 20) + 'px';      // 不能给offsetLeft赋值,是只读的属性      

td.appendChild(new_select);    

posted @ 2008-08-11 09:28 大宋提刑官 阅读(1841) 评论(0) 编辑
一直以为offsetParent和parentElement是一回事,最近在做web控件才发现原来的理解是大错特错。
 
 parentElement 在msdn的解释是Retrieves the parent object in the object hierarchy.
 
 而offsetParent在msdn的解释是Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object. 这个解释有些模糊。我们再来看看他的remarks
 
 Most of the time the offsetParent property returns the body object.
 
 大多说offsetParent返回body
 
 
 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.
 
 对于IE 5.0以上,TD的offsetParent返回Table。
 
 但是msdn并没有讨论在页面td元素中存在绝对/相对定位时offsetParent的值。
 
 以下是我个人总结的规律
 
 在td中的元素会把第一个绝对/相对定位的hierarchy parent当作offsetParent,如果没有找到需要分三种情况讨论
 
   一,如果该元素没有绝对/相对定位,则会把td当作offsetParent
 
   二,如果该元素绝对/相对定位并且table没有绝对/相对定位,则会把body当作offsetParent
 
   三,如果该元素绝对/相对定位并且table绝对/相对定位,则会把table当作offsetParent
 
 看一下示例代码
 
 1.<BODY >
 <TABLE BORDER=1 ALIGN=right>
   <TR>
     <TD ID=oCell><div id="parentdiv" style="position:relative" >parentdiv<div id="sondiv">sondiv</div></div></TD>
   </TR>
 </TABLE>
 
 
 运行结果parentdiv.offsetParent.tagName IS "body"
 
                 sondiv.offsetParent.id     IS "parentdiv"
 
 2.<BODY >
 <TABLE BORDER=1 ALIGN=right>
   <TR>
     <TD ID=oCell><div id="parentdiv" style="position:relative" >parentdiv<div id="sondiv" style="position:relative">sondiv</div></div></TD>
   </TR>
 </TABLE>
 
 运行结果parentdiv.offsetParent.tagName IS "body"
 
                 sondiv.offsetParent.id     IS "parentdiv"
 
 3.<BODY >
 <TABLE BORDER=1 ALIGN=right>
   <TR>
     <TD ID=oCell><div id="parentdiv" >parentdiv<div id="sondiv" style="position:relative">sondiv</div></div></TD>
   </TR>
 </TABLE>
 
 
 运行结果parentdiv.offsetParent.tagName IS "TD"
 
                 sondiv.offsetParent.tagName    IS "body"
 
 4.<BODY >
 <TABLE BORDER=1 ALIGN=right>
   <TR>
     <TD ID=oCell><div id="parentdiv" >parentdiv<div id="sondiv">sondiv</div></div></TD>
   </TR>
 </TABLE>
 
 
 运行结果parentdiv.offsetParent.tagName IS "TD"
 
                 sondiv.offsetParent.tagName    IS "TD"
 
 5.<BODY >
 <TABLE BORDER=1 ALIGN=right style="position:relative">
   <TR>
     <TD ID=oCell><div id="parentdiv" style="position:relative" >parentdiv<div id="sondiv" style="position:relative">sondiv</div></div></TD>
   </TR>
 </TABLE>
 
 运行结果parentdiv.offsetParent.tagName IS "Table"
 
                 sondiv.offsetParent.tagName    IS "parentdiv"
posted @ 2008-08-11 09:11 大宋提刑官 阅读(1250) 评论(0) 编辑