2 回答
TA贡献1803条经验 获得超3个赞
不会呀,估计你代码有其他问题
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
body{margin:0;padding:0;position:relative;}
#box{position:relative;width:200px;height:200px;background:#ddd;margin:25px auto;}
#test{width:100px;height:100px;background:#000;margin:0 auto;}
.big{position:relative;width:250px;height:250px;background:#dd4215;margin:20px auto;overflow:hidden;}
</style>
<div class="big">
<div id="box">
<div id="test"></div>
</div>
</div>
<button>点我看offsetTop</button>
<script>
var oTest = document.getElementById('test');
var oButton = document.getElementsByTagName('button')[0];
oButton.onclick = function () {
alert('top:' + oTest.offsetTop + '\r\nleft:' + oTest.offsetLeft)
};
</script>
</body>
</html>
添加回答
举报