zoukankan      html  css  js  c++  java
  • JavaScript系列---【常用鼠标事件】

    鼠标事件类型

    1.1 onclick 单击

    代码示例:

    document.getElementById("box").onclick = function(){
     alert('onclick');
    }
    

    1.2 ondblclick 双击

    代码示例:

    document.getElementById("box").ondblclick = function(){ 
     alert('ondblclick');
     } 
    

    1.3 onmouseover 鼠标移入

    代码示例:

    document.getElementById("box").onmouseover = function () { 
     alert('onmouseover'); 
    }
    

    1.4 onmouseout 鼠标移出

    代码示例:

    document.getElementById("box").onmouseout = function () { 
     alert('onmouseout');
     }
    

    1.5 onmouseenter 鼠标进入

    代码示例:

    document.getElementById("box").onmouseenter = function () {
     alert('onmouseenter'); 
     }
    

    1.6 onmouseleave 鼠标离开

    代码示例:

    document.getElementById("box").onmouseleave = function () { 
     alert('onmouseleave');
     }
    

    1.7 onmousemove 鼠标移动

    代码示例:

    document.getElementById("box").onmousemove = function () { 
     alert('onmousemove'); 
     }
    

    1.8 oncontextmenu 鼠标右击

    代码示例:

    document.getElementById("box").oncontextmenu = function () { 
     alert('oncontextmenu'); 
     }
    

    1.9 onmousedown 鼠标按下

    代码示例:

    document.getElementById("box").onmousedown = function () { 
     alert('onmousedown'); 
     }
    

    2.0 onmouseup 鼠标抬起

    代码示例:

    document.getElementById("box").onmouseup = function () { 
     alert('onmouseup'); 
     }
    

    2.1 注意事项

    鼠标移入及进入 - 移出及离开的区别:

    onmouseover及onmouseout 不仅会将当前这个元素上这个事件触发还会将父元素对应的这个事件触发

    <body>
        <!-- 父级元素 -->
        <div id="father">
            <!-- 子集元素 -->
            <div id="son"></div>
        </div>
        <script>
         document.getElementById("father").onmouseover = function(){
         console.log("father");//打印father
         }
    
         document.getElementById("son").onmouseout = function(){
         console.log("son");//打印son father
         }
    
         document.getElementById("father").onmouseenter = function(){
         console.log("father");
         }
    
         document.getElementById("son").onmouseenter = function(){
         console.log("son");
         }
        </script>
    </body>
    
    

    onmouseenter及onmouseleave 只会触发当前这个元素上的这个事件,不会触发父级元素当前这个事件

       document.getElementById("father").onmouseenter = function(){
       console.log("father");//打印father
       }
    
      document.getElementById("son").onmouseleave = function(){
      console.log("son");//打印son
      }
        
    
  • 相关阅读:
    centos 6.5 查看、开启,关闭 端口
    centos 安装 nginx
    centos 安装 svn
    centos 安装 maven
    (转)不停止Nginx服务的情况下平滑变更Nginx配置
    记录1---python+linux+vim之while循环语句使用
    记录1---linux系统之创建用户,用户登录,查看用户名,切换用户登录,退出登录
    记录——Fiddler5.0 中文版 绿色免费版 汉化破解版安装
    fiddler笔记1---fiddler的安装 和 证书安装 以及 证书导出失败问题解决
    fiddler笔记2--fiddler工具界面的功能使用与介绍
  • 原文地址:https://www.cnblogs.com/chenhaiyun/p/14433542.html
Copyright © 2011-2022 走看看