事件对象
<style>
#areaDiv{
width: 300px;
height: 100px;
border: 1px black solid;
margin: 20px;
}
#showMsg{
width:300px;
height: 50px;
border: 1px black solid;
margin: 20px;
}
</style>
<script>
/*
在IE8中不可用,所以先用if语句判断一下
当鼠标在areaDiv中移动时,在showMsg中显示鼠标的坐标
*/
window.onload = function(){
var areaDiv = document.getElementById("areaDiv");
var showMsg = document.getElementById("showMsg");
//onmousemove:该事件在鼠标移动时,会发生
//在showMsg中显示鼠标的坐标
/*
--事件对象
当事件的响应函数被触发时,游览器都会将一个事件对象作为实参传进响应函数
clientX:可以获取鼠标指针的水平坐标
clientY:可以获取鼠标指针的垂直坐标
*/
areaDiv.onmousemove = function(event){
if(!event){
event = window.event;
};//用于IE8
event = event || window.event//解决兼容性
var x = event.clientX;
var y = event.clientY
//alert("我动了");
showMsg.innerHTML = "x = "+x +",y = "+y;
};
};
</script>
</head>
<body>
<div id="areaDiv"></div>
<div id="showMsg"></div>
</body>
</html>