<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>왼쪽으로 돌아갑니다. <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>위로 돌아가기; <BR>} <br><br>function DisplayCoord(event){ <BR>var top,left,oDiv; <BR>oDiv=document.getElementById("demo"); <BR>top=getY(oDiv); <BR>왼쪽=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 스타일="배경-색상:#000000;색상:#0011FF;너비:300px;높이:300px;위치:절대;상단:80px;왼쪽:90px;마진:0px; 테두리: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