2 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個贊
不會呀,估計(jì)你代碼有其他問題
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標(biāo)題文檔</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>點(diǎn)我看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>
添加回答
舉報(bào)