首页主机资讯onmousemove事件如何获取坐标

onmousemove事件如何获取坐标

时间2024-07-02 22:48:03发布访客分类主机资讯浏览251
导读:可以使用event对象中的clientX和clientY属性来获取鼠标在页面中的坐标位置。示例代码如下: <!DOCTYPE html> <html> <head> <title>获取鼠标坐标...

可以使用event对象中的clientX和clientY属性来获取鼠标在页面中的坐标位置。示例代码如下:

<
    !DOCTYPE html>
    
<
    html>
    
<
    head>
    
<
    title>
    获取鼠标坐标<
    /title>
    
<
    script>

function showCoords(event) {
    
  var x = event.clientX;
    
  var y = event.clientY;
    
  document.getElementById("coords").innerHTML = "鼠标坐标:(" + x + ", " + y + ")";

}
    
<
    /script>
    
<
    /head>
    
<
    body onmousemove="showCoords(event)">
    
<
    p id="coords">
    <
    /p>
    
<
    /body>
    
<
    /html>
    

在上面的示例中,onmousemove事件会触发showCoords函数,并将event对象传递给该函数。showCoords函数中通过event.clientX和event.clientY获取鼠标的坐标位置,并将坐标显示在页面上。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: onmousemove事件如何获取坐标
本文地址: https://pptw.com/jishu/685741.html
onmousemove如何判断鼠标方向 onmousemove能应用于SVG图形吗

游客 回复需填写必要信息