得到鼠標座標

<html>

<head>
<script language="javascript" type="text/javascript">


var x,y;
function mouse_0(a)
{

 x=document.body.scrollLeft+event.clientX;

 y=document.body.scrollTop+event.clientY;

//以上document.body.scrollLeft當頁面出現滾動是獲得座標event.clientY是不滾動時獲得本頁面座標。

document.getElementById("dd").style.left=x;
document.getElementById("dd").style.top=y;
document.getElementById("dd").innerText="A(x="+x+";y="+y+")";
}
//DIV一定要加上position: absolute;

//position: absolute是CSS中的絕對定位方法,使用life,right,top,bottom等屬性進行絕對定位
</script>
<title></title>

</head>

<body onclick="mouse_0()">

<div  id="dd" style="position: absolute;" >A</div>


</body>

</html> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章