<div class="codetitle"> <span><a style="CURSOR: pointer" data="2454" class="copybut" id="copybut2454" onclick="doCopy('code2454')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code2454"> <br><HEAD> <br><TITLE> JS获取DIV相对坐标</TITLE> <br><script type="text/javascript"><!-- <BR>function getX(obj){ <BR>var parObj=obj; <BR>var left=obj.offsetLeft; <BR>while(parObj=parObj.offsetParent){ <BR>left =parObj.offsetLeft; <BR>} <BR>return left; <BR>} <br><br>function getY(obj){ <BR>var parObj=obj; <BR>var top=obj.offsetTop; <BR>while(parObj = parObj.offsetParent){ <BR>top =parObj.offsetTop; <BR>} <BR>return top; <BR>} <br><br>function DisplayCoord(event){ <BR>var top,left,oDiv; <BR>oDiv=document.getElementById("demo"); <BR>top=getY(oDiv); <BR>left=getX(oDiv); <BR>document.getElementById("mp_x").innerHTML = (event.clientX-left document.documentElement.scrollLeft) -2 "px"; <BR>document.getElementById("mp_y").innerHTML = (event.clientY-top document.documentElement.scrollTop) -2 "px"; <BR>} <BR>// --></script> <br></HEAD> <br><br><BODY style="margin:40px;" mce_style="margin:40px;"> <br><div style="background-color:#000000;color:#0011FF;width:300px;height:300px;position:absolute;top:80px;left:90px;margin:0px; border:0px;" id="demo" onmousemove="DisplayCoord(event)"> <br>我是DIV,经测试,有2PX的误差... <br></div> <br>当前鼠标坐标为: <br>X:<span id="mp_x"></span> <br>Y:<span id="mp_y"></span> <br></body> <br></BODY> <br><br></HTML> <br> </div> <br><br>如果不对,可以试试将 <br><br>document.documentElement.scrollLeft <br><br>替换成 <br><br>document.body.scrollLeft