W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <style> div { width: 200px; height: 100px; border: 1px solid black; } </style> </head> <body> <div onmousemove="showCoords(event)" onmouseout="clearCoor()"></div> <p>将鼠标悬停在上面的矩形上,可获取鼠标指针的水平和垂直坐标。</p> <p id="demo"></p> <script> function showCoords(event) { var x = event.clientX; var y = event.clientY; var coor = "X coords: " + x + ", Y coords: " + y; document.getElementById("demo").innerHTML = coor; } function clearCoor() { document.getElementById("demo").innerHTML = ""; } </script> </body> </html>