我正在将一个外部网页(例如此示例中的www.duckduckgo.com)加载到我的网页div中。我想要在div内和外获取鼠标的X和Y位置,但是当我在div内时,似乎网页会阻止onmousemove事件触发。但是,在进入div时,onmouseover事件只会触发一次。
以下是说明问题的示例代码:
我该如何获取鼠标在整个网页上的X和Y坐标位置(即不仅限于在包含外部源的div上)?我尝试在mouseEvent函数的开头添加
以下是说明问题的示例代码:
function mouseEvent(event) {
var x = event.clientX;
var y = event.clientY;
document.getElementById('label').innerHTML = 'X=' + x + ' Y=' + y;
}
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
overflow: hidden;
margin-left: 0px;
margin-top: 0px;
margin-bottom: 0px;
margin-right: 0px;
}
#form1 {
height: 100%;
width: 100%;
}
#pageDiv {
height: 100%;
width: 100%;
}
#page {
height: 100%;
width: 100%;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="pageDiv">
<label id="label">hello</label>
<object id="page" type="text/html" data="https://duckduckgo.com/" onmousemove="mouseEvent(event)" onmouseover="mouseEvent(event)">
</object>
</div>
</form>
</body>
</html>
event.preventDefault();
,但这并没有帮助解决问题。我猜测外部网页正在夺取我的focus。这是真的吗?有没有办法实时更新X和Y坐标?
window
上。可能是同源策略阻止了来自污染对象的事件。 - adeneo$(window).mousemove(mouseMove(event));
。问题仍然存在。 - Code Doggo