<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> <br> JS取得DIV相對座標 <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){ <Oj>var par =obj; <BR>var top=obj.offsetTop; <BR>while(parObj = parObj.offsetParent){ <BR>top =parObj.offsetTop; <BR>} <BR>返回頂部; ><BR>function DisplayCoord(event){ <BR>var top,left,oDiv; <br>oDiv=document.getElementById("demo"); <br>top=getY(oDiv); <BR>left=X (oDiv); <BR>document.getElementById("mp_x").innerHTML = (event.clientX-left document.documentElement.scrollLeft) -2 "px"; <BR>document.getElementById("mp_MLy").in (event.clientY-top document.documentElement.scrollTop) -2 "px"; <BR>} <BR>// --></腳本> <BR> <BR><BR><BODY style ="margin:40px;" mce_style="邊距: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(事件)"> <br>我是DIV,經過測試,有2PX的托盤... <br> </script> </div> <br>目前滑鼠座標為: <br>X:<span id="mp_x"></span> <br>Y:<span id="mp_y"></span> <br> <br>身體> <br><br>