<style>
* {
margin: 0;
padding: 0;
}
#box{
margin: 100px 0 0 100px;
width: 200px;
height: 200px;
border: 1px solid #ccc;
}
</style>
<body>
<div id="box"></div>
<script>
var box = document.getElementById('box');
box.onclick = function (e) {
e = e || window.event;
// 鼠标在盒子中的坐标 = 鼠标在页面中的位置 - 盒子的页面的位置
var x = getPage(e).pageX - this.offsetLeft;
var y = getPage(e).pageY - this.offsetTop;
console.log(x,y);
}
</script>
网友评论