以下代码取自“JavaScript by Example Second Edition”。
我认为代码
if (!e) var e = window.event; // Internet Explorer
应该
if (!e) e = window.event; // Internet Explorer
你怎么看?这样对吗?或者代码应该保持原样?
<html>
<head>
<title>Mouse Coordinates</title>
<script type="text/javascript">
function getCoords(e) {
var x = 0; // x and y positions
var y = 0;
if (!e) var e = window.event; // Internet Explorer
if (e.pageX || e.pageY) { // Firefox
x = e.pageX;
y = e.pageY;
}
else if (e.clientX || e.clientY) {
x = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
// x and y contain the mouse position
// relative to the document
alert(x + ", " + y);
}
</script>
</head>
<body>
<div style="background-color: aqua; position: absolute; top: 50px"
onmouseover="return getCoords(event);">
<h1>Mouse positions are relative to the document, not the
<div> container</h1>
</div>
</body>
</html>