运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往空间
<!DOCTYPE html> <html> <style> div { width: 200px; height: 100px; border: 1px solid black; } </style> <body> <h1>HTML DOM Events</h1> <h2>The onmousemove Event</h2> <div onmousemove="myFunction(event)" onmouseout="clearCoor()"></div> <p>Mouse over the rectangle above, and get the coordinates of your mouse pointer.</p> <p>When the mouse is moved over the div, the p element will display the horizontal and vertical coordinates of your mouse pointer, whose values are returned from the clientX and clientY properties on the MouseEvent object.</p> <p id="demo"></p> <script> function myFunction(e) { let x = e.clientX; let y = e.clientY; let coor = "Coordinates: (" + x + "," + y + ")"; document.getElementById("demo").innerHTML = coor; } function clearCoor() { document.getElementById("demo").innerHTML = ""; } </script> </body> </html>