当我意识到这个小鼠标跟踪示例时,我只是将这个小鼠标跟踪示例放在一起,它会在 chrome 控制台中引发数千个错误。
Uncaught TypeError: Cannot read property 'clientX' of undefined
AFAIK 这意味着,chrome 找不到该属性所属的对象。但是如果我打开这个例子,它会很好地显示正确的坐标。请帮助我摆脱这些无用的错误。
<html>
<head>
<style type="text/css">
#tracking {
width: 300px;
height: 300px;
background-size: 300px 300px;
background-color: #00F;
left: 100px;
top: 100px;
position: absolute;
}
</style>
<script type="text/javascript">
var mX, mY;
var track;
document.onmousemove = update;
function trackload()
{
track = document.getElementById("tracking");
setTimeout("update()",10);
}
function update(e)
{
mX = e.clientX;
mY = e.clientY;
if (track) {
track.innerHTML = "X: " + mX + " Y: " + mY + " deg: " + (Math.atan(mY / mX) * (180/Math.PI));
track.style.cssText = "-webkit-transform: rotate(" + (Math.atan(mY / mX) * (180/Math.PI)) + "deg);";}
setTimeout("update()",10);
}
</script>
</head>
<body>
<div id="tracking" onclick="trackload()"></div>
</body>