2017.5.11

mousedown:鼠标按下才发生

mouseup:鼠标按下松开时才发生

mouseenter和mouseleave效果和mouseover mouseout效果差不多;但存在区别,区别见代码解析:

css代码:

复制代码
<style>
            .cc,.dd{
                height: 80px;
                 300px;
                border: 1px solid black;
            }
            .ff,.ee{
                height: 200px;
                 300px;
                background-color: darkgrey;
                border:1px solid red;
                text-align: center;    
            }
        </style>
复制代码

html代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<body>
         
        <div class="aa">1、please press down your mouse button</div><br />
        <div class="bb">2、please press up your mouse button</div><br />
         
        <div class="cc">
         
            3、mouseenter:颜色变红
            mouseleave:颜色变灰
         
        </div><br />
         
        <div class="dd">
            4、mouseover:颜色变黄
            mouseout:颜色变灰
        </div><br />
         
        <div class="ff"> 5、<p style="">mouseout事件在鼠标离开任意一个子元素及选的元素时触发</p><span></span> </div><br />
        <div class="ee"> 6、<p style="">mouseleave事件只在鼠标离开选取的的元素时触发。</p><span></span>  </div>   
    </body>

  

jqery代码:

复制代码
<script>
//当鼠标按下时会显示
        $(".aa").mousedown(function(){
            $(this).after("<p>when mouse button press down</p>")
        });
//当鼠标按下松开时发生的        
        $(".bb").mouseup(function(){
            $(this).after("<p>when mouse button press up</p>")
        });
//当鼠标enter/leave
        $(".cc").mouseenter(function(){
            $(".cc").css("background-color","red")          
        });
         $(".cc").mouseleave(function(){
            $(".cc").css("background-color","grey")          
        });    
 //当鼠标mouseover/mouseout
       $(".dd").mouseover(function(){
           $(".dd").css("background-color","yellow")
       });
       $(".dd").mouseout(function(){
           $(".dd").css("background-color","grey")
       });
 //mouseleave 与 mouseout 的区别   
 x=0;
 y=0;
 $(".ff").mouseout(function(){
     $(".ff span").text(x+=1);
 })
 $(".ee").mouseleave(function(){
     $(".ee span").text(y+=1);
 })
 //mouseenter,mouseover同理
 //mouseover 事件在鼠标移动到选取的元素及其子元素上时触发 。
//mouseenter 事件只在鼠标移动到选取的元素上时触发。
</script>
原文地址:https://www.cnblogs.com/nzhcww/p/6839643.html