首页 > 代码库 > 鼠标移动事件--JavaScript

鼠标移动事件--JavaScript

鼠标移动事件

<html>
<head>
<title>鼠标移动事件</title>
</head>

<style type="text/css">
div{
    width:300px;
    height:300px;
    border:1px solid red;
}

</style>

<script type="text/javascript">

//当鼠标在div上移动时,显示鼠标的坐标
function fun(e){
    var x = e.clientX;
    var y = e.clientY;
    
    //显示在文本框中
    var txt = document.getElementById("txt");
    txt.value = x + ":" + y;
}



</script>

<body>
    <input type="text" name="" id="txt">
    <div onm ousemove = "fun(event)"> </div>
</body>

</html>

 

鼠标移动事件--JavaScript