<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; padding:0}
div {padding: 40px 50px;}
#div1 {background: red;position: relative;}
#div2 {background: green; position: relative;}
#div3 {background: orange; position: relative;}
</style>
<script>
window.onload = function() {
var oDiv3 = document.getElementById('div3');
var iTop = 0;//初始化
var obj = oDiv3;//默认为oDiv3
while (obj) {
iTop += obj.offsetTop;//offsetTop累加在变化
obj = obj.offsetParent;//父级也在变
//alert(obj + ' : ' + obj.id); //弹出父级
//alert( obj.id + ' : ' + obj.offsetTop );//弹出父级及offsetTop 的值
}
alert( iTop );//弹出80*{ margin:0; padding:0} 初始化了的结果 若不写初始化边距为0的情况下 在火狐谷歌360ie8以上弹出88 默认边距8px 在ie7弹出95默认边距15
}
</script>
</head>
<body id="body1">
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
</body>
</html>

最新文章

  1. HTML5实现网页的全屏切换
  2. 我的jQuery源码读后感
  3. GCD 开发
  4. 微信支付 - V3支付问题
  5. IQ推理:红眼睛和蓝眼睛
  6. (转).net项目技术选型总结
  7. Gradle sourceCompatibility has no effect to subprojects(转)
  8. 除了使用URLSearchParams处理axios发送的数据,但是兼容性不好,其他的兼容方法
  9. DOM节点的创建
  10. Python编程从入门到实践笔记——列表简介
  11. UE4 打包C++项目到win32平台报错 could not find mspdbcore.dll
  12. 集合框架之map
  13. springboot热部署(二)——springboot热部署与发布
  14. Backpack VI
  15. WebService接口定义及调用
  16. C#复习题(概念) --C#学习笔记
  17. overflow 在float浮动标签里的作用
  18. MySQL笔记(五)MySQL 角色与SQL CHECK约束
  19. 阿里云上部署tomcat启动后,通过http不能访问
  20. dj cookie &amp; session组件

热门文章

  1. windbg的高级玩法
  2. 面向服务的架构SOA
  3. TextView展开和收回
  4. 【MyEcplise hibernate tools】hibernate tools的使用以及错误
  5. Android 实现ListView中Item被单击后背景色保持高亮
  6. css3 -- 背景图处理
  7. js运动框架tween
  8. sqlserver数据库 去除字段中空格,换行符,回车符(使用replace语句)
  9. The 2015 China Collegiate Programming Contest A. Secrete Master Plan hdu5540
  10. Codeforces Round #235 (Div. 2) A. Vanya and Cards