W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <style> div { width: 500px; height: 300px; border: 1px solid black; text-align: center; } </style> </head> <body> <p>单击下面的 div 元素可获取鼠标指针在单击时的 x(水平)和 y(垂直)坐标。</p> <div onclick="showCoords(event)"><p id="demo"></p></div> <p><b>提示:</b>请尝试单击 div 中的不同位置。</p> <script> function showCoords(event) { var cX = event.clientX; var sX = event.screenX; var cY = event.clientY; var sY = event.screenY; var coords1 = "client - X: " + cX + ", Y coords: " + cY; var coords2 = "screen - X: " + sX + ", Y coords: " + sY; document.getElementById("demo").innerHTML = coords1 + "<br>" + coords2; } </script> </body> </html>