我想在页面中显示鼠标坐标,当我不声明 DOCTYPE 时它可以工作,但是当我声明 DOCTYPE 时它不会!你能帮我解决这个问题吗?这是我的代码:
<html>
<head>
<title>problem</title>
</head>
<body>
text...
<div id="show"></div>
<script>
document.body.onmousemove = function(event) {
document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
}
</script>
</body>
</html>
在上面的代码中,我可以毫无问题地获得 y 坐标,但是当我添加 doctype 时,它没有正确显示 y 坐标:
<DOCTYPE html>
<html>
<head>
<title>problem</title>
</head>
<body>
text...
<div id="show"></div>
<script>
document.body.onmousemove = function(event) {
document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
}
</script>
</body>
</html>
编辑
这是我的代码,现在可以完美运行。谢谢你们:
<!DOCTYPE html>
<html>
<head>
<title>problem</title>
</head>
<body>
text...
<div id="show"></div>
<script>
if (document.addEventListener) {
document.addEventListener('mousemove', function(event) {
document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
});
} else {
document.attachEvent("onmousemove", function(event) {
document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
});
}
</script>
</body>
</html>