mouseover事件mouseenter事件mouseleave事件和mouseout事件之间的区别与联系

两者定义

mouseover:鼠标覆盖
mouseenter:鼠标进入
mouseleave:鼠标移出
mouseout:鼠标移出

两两区别

  1. 既会捕获也会冒泡
    mouseover鼠标指针穿过被选元素或是其子元素都会触发
    mouseout鼠标指针移出被选元素或是其子元素都会触发
  2. 只会捕获不会冒泡
    mouseenter只有鼠标指针穿过被选元素才会触发
    mouseleave只有鼠标指针移出被选元素才会触发

例子

我们同时对大div加上这四个属性,然后从左向右移动鼠标

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
   #big{
    height: 500px;
    width: 500px;
    background-color: aqua;
    margin:auto;
    }
    #small{
    height: 200px;
    width: 200px;
    background-color: rgb(36, 37, 37);
    margin:auto;
    }
</style>
<body>
    <div id="big">
        <div id="small">
    
        </div>
    </div>
    <script>
        let big=document.getElementById('big')
        big.onmouseover=function()
        {
            console.dir('mouseover')
        }
        big.onmouseout=function()
        {
            console.dir('mouseout')
        }
        big.onmouseenter=function()
        {
            console.dir('mouseenter')
        }
        big.onmouseleave=function()
        {
            console.dir('mouseleave')
        }
        </script>
    
</body>
</html>

我们对鼠标移动的四个交叉点进行标记
在这里插入图片描述
效果展示:
在这里插入图片描述
在一号节点鼠标进入选中元素触发了:
mouseover
mouseenter
在二号节点鼠标离开选中元素并进入选中元素的子元素触发了:
mouseout
mouseover
在三号节点鼠标离开选中元素的子元素并进入选中元素触发了:
mouseout
mouseover
在四号节点鼠标离开选中元素触发了:
mouseout
mouseleave

小结

  • mouseovermouseout成对,mouseentermouseleave成对

简记:两个后面含o的一对是全能;两个后面含se的一队只会一半。

  • mouseover触发优先级高于mouseentermouseout触发优先级高于mouseleave

简记:先离开后进入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值