我在获取mousedown的offsetX时遇到了问题。以下是我的代码:
我在鼠标按下DIV区域时得到了想要的正确结果,但当鼠标悬停在段落区域时,结果却错误了。我不明白为什么会这样,因为事件是父元素DIV元素的事件。
获取结果 情况1:当我的鼠标悬停在DIV元素上时 顶部:17像素,左侧:61像素
情况2:当我的鼠标悬停在段落元素上时 顶部:11像素,左侧:9像素
<!DOCTYPE html>
<html>
<body>
<div id="myP" onmousedown="mouseDown()">
Click the text! The mouseDown() function is triggered when the mouse button is pressed down over this paragraph,
<p style="margin-left:50px">
and sets the color of the text to red. The mouseUp() function is triggered when the mouse button is released,
</p>
and sets the color of the text to green.
</div>
<script>
function mouseDown() {
var left = event.offsetX;
var top = event.offsetY;
console.log(top+"::"+left);
}
</script>
</body>
</html>
我在鼠标按下DIV区域时得到了想要的正确结果,但当鼠标悬停在段落区域时,结果却错误了。我不明白为什么会这样,因为事件是父元素DIV元素的事件。
获取结果 情况1:当我的鼠标悬停在DIV元素上时 顶部:17像素,左侧:61像素
情况2:当我的鼠标悬停在段落元素上时 顶部:11像素,左侧:9像素