<html>
<head>
<meta http-equiv="content-type" content="text/htmlcharset=GB2312" />
<title>CSS和Js演示网页绝对定位与相对定位</title>
<!-- 通用样式表 -->
<style>
* { font-size:12pxfont-family:宋体, Arial} /*规定了所有的字体样式*/
body { overflow:autoborder-style:nonemargin:0pxpadding:0px}
.tools { position:absolutetop:180pxleft:10px}
#abs { position:absoluteleft:10pxtop:10pxwidth:150pxheight:150pxbackground-color:buttonfacepadding:10px}
#rel { position:relativeleft:170pxtop:40pxwidth:150pxheight:120pxbackground-color:buttonfacepadding:10px}
</style>
<script>
function logEvent(){
$("ipt_x").value = event.x
$("ipt_y").value = event.y
$("ipt_x2").value = event.clientX
$("ipt_y2").value = event.clientY
}
function $(str){ return(document.getElementById(str))}
window.onload = function(){
document.onmousemove = logEvent
}
</script>
</head>
<body>
<div id="abs">绝对定位</div>
<div id="rel">相对定位</div>
<div class="tools">
event.x <input id="ipt_x" size="15" >
event.y <input id="ipt_y" size="15" >
<br/>
event.clientX <input id="ipt_x2" size="9" >
event.clientY <input id="ipt_y2" size="9" >
</div>
</body>
</html>
首先标签position要设置成相对、绝对定位然后获取元素
var dom = document.getElementById("元素id")//你可以用其它方法获得
alert(dom.offsetLeft)//left
alert(dom.offsetTop)//top
这样就获得了,但是前提是它外层套的标签没有设置成相对、绝对定位的,否则位置是相对于那个标签的位置,这个就需要获取offsetParent 判断父级是谁了