<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> 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("デモ"); <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> <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="デモ" onmousemove="DisplayCoord(イベント)"> <br>私は DIV、经测试、2PX の误差あり... <br> </div> <br>現在のネズミ标坐标: <br>X:<span id="mp_x"></span> <br>Y:<span id="mp_y"></span> <br></body> <br>