最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

獲取任意Html元素與body之間的偏移距離 offsetTop、offsetLeft (For:IE5+ FF1 )[

 更新時間:2006年12月22日 00:00:00   作者:  
問題:
如何取到頁面中任意某個Html元素與body元素之間的偏移距離?

offsetTop和offsetLeft 這兩個屬性,IE 、Opera和Firefox對它倆的解釋存在差異:
IE5.0+ 、Opera8.0+: offsetTop和offsetLeft 都是相對父級元素
Firefox1.06: offsetTop和offsetLeft 都是相對于body元素

因此:
(1)在FF下直接使用offsetTop和offsetLeft,就可以取到頁面中任意某個Html元素與body元素之間的偏移距離;
(2)在IE、Opera下則比較麻煩:
需要首先取到該Html元素與body元素之間所有Html元素,計算各自的offsetTop和offsetLeft,然后再累加。
即:從該Html元素開始,遍歷至body,在遍歷的過程中,如果某個HTML元素的CSS設(shè)置了borderWidth的話,則borderWidth不是算在offsetTop和offsetLeft內(nèi)的--因此在遍歷的過程中,還需要累加上:
obj.currentStyle.borderLeftWidth、obj.currentStyle.borderTopWidth

下面這段測試代碼已經(jīng)解決上述問題,兼容IE5、FF1,但在Opera8下無效

實例代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<head>
<title> 代碼實例:獲取任意Html元素與body之間的偏移距離 offsetTop、offsetLeft </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<meta name="author" content="楓巖,CNLei.y.l@gmail.com">
<style type="text/css" media="all">
body,p {margin:0;padding:0;font-size:12px;}
body {float:left;width:100%;}
ul,ul li {margin:0;padding:0;list-style:none;padding:0;}
ul li input {border:1px solid #ccc;}
#Bd {
background:#FFE8D9;
float:left;
padding:20px;
border:10px solid #f90;/*該值在IE下還是取不到*/
width:100%;
}
#BS {
padding:20px;
float:left;
background:#58CB64;
}
#BS ul {border:20px solid #DDF1D8;}
#BM {
margin-top:100px;
float:right;
width:300px;
background:#fff;
}
</style>
<script type="text/javascript">
var w3c=(document.getElementById)? true:false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5=(w3c && ie)? true : false;
var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
var op8=(navigator.userAgent.toLowerCase().indexOf("opera")==-1)? false:true;

function Obj(o){
 return document.getElementById(o)?document.getElementById(o):o;
}

function GetXYWH(o){
var nLt=0;
var nTp=0;
 var offsetParent = o;
 while (offsetParent!=null && offsetParent!=document.body) {
 nLt+=offsetParent.offsetLeft;
 nTp+=offsetParent.offsetTop;
 if(!ns6){
 parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
 parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
 }
 offsetParent=offsetParent.offsetParent;
 //alert(offsetParent.tagName);
 }
alert("ID:"+o.id+"\n\nL:"+nLt+" T:"+nTp+"\nW:"+o.offsetWidth+" H:"+o.offsetHeight);
}
</script>
</head>
<body>
<p style="height:100px;margin:0;padding:0;background:#00f;color:#fff;line-height:100px;text-align:center;">此色塊高:100px;</p>
<div id="Bd">
<div id="BS">
<ul>
<li><input type="text" value="無法取到橙黃色的邊框線寬度(border:10px solid #f90;)" onclick="GetXYWH(this);" size="60" /></li>
<li><input type="text" value="GetXYWH(this);" onclick="GetXYWH(this);" size="60" /></li>
<li><input type="text" value="GetXYWH(this);" onclick="GetXYWH(this);" size="60" /></li>
<li><input type="text" value="GetXYWH(this);" onclick="GetXYWH(this);" size="60" /></li>
<li><input type="text" value="GetXYWH(Obj('BM'));" onclick="GetXYWH(Obj('BM'));" size="60" /></li>
</ul>
</div><!--BS-->
<div id="BM" onclick="GetXYWH(this);">
<p>測試</p>
<p>測試</p>
<p>測試</p>
<p>測試</p>
<p>測試</p>
</div><!--BM-->
</div>
</body>
</html>

相關(guān)文章

最新評論

四会市| 宁德市| 南京市| 城口县| 西乌珠穆沁旗| 新巴尔虎右旗| 额尔古纳市| 武胜县| 城固县| 康平县| 铜陵市| 绍兴市| 巢湖市| 临武县| 汤原县| 海淀区| 贡山| 武山县| 马龙县| 德阳市| 辰溪县| 新巴尔虎左旗| 丹阳市| 遵义市| 吕梁市| 资源县| 新营市| 东台市| 株洲县| 阿克陶县| 墨竹工卡县| 嘉禾县| 富源县| 三原县| 丹东市| 夏津县| 南城县| 尉犁县| 岳普湖县| 南汇区| 郴州市|