Blogger Information
Blog 17
fans 1
comment 1
visits 16537
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取鼠标在div中的位置
雷国恩的博客
Original
1082 people have browsed it

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style type="text/css">
   #main{
    width: 500px;height:400px;
    margin: 100px auto 20px;
    border: 2px solid red;
   }
   p{
    width: 500px;height: 50px;
    margin: 0 auto;
    border: 2px solid green;
    line-height: 50px;
          
   }
 </style> 
</head>
<body>
 <div id="main"></div>
 <p>坐标为:<b id="show">(0,0)</b></p>
 <script type="text/javascript">
   window.onload=function(){
    var main=document.getElementById('main');
    var show=document.getElementById('show');
    main.onmousemove=function(event){
      var e=event || window.event;
      var m_x=e.clientX;
      var m_y=e.clientY;

      var mainL=this.offsetLeft;
      var mainT=this.offsetTop;
      
      var x=m_x-mainL;
      var y=m_y-mainT;
      show.innerHTML="("+x+","+y+")";
    }


   }
 
 </script>
</body>
</html>

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post