mouseover和mouseenter的异同区别

mouseover和mouseenter的不同点:
不同点有两方面:

事件的触发时机
是否支持冒泡

一、事件的触发时机?
下面是我自己做的测试:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding:0;
        }
        ul{
            width: 300px;
            height: 200px;
            padding-top: 10px;
            padding-left: 50px;
            background-color: #0ff;
        }
        ul li{
            width: 200px;
            height: 50px;
            list-style: none;
            background-color: #0b6;
            margin-top: 30px;
            text-align: center;
            line-height: 50px;
        }
    </style>
</head>
<body>
    <ul>
        <li class="a">
            我是li.a
        </li>
        <li class="b">
            我是li.b
        </li>
    </ul>
    <script src="jquery.min.js"></script>
    <script>
        $(function(){
            $("ul").mouseenter(function(){
                console.log("经过一次");                
            })
        })
    </script>
</body>
</html>

mouseover测试:
由图看出:当光标进入ul和子元素li的时候都会触发mouseover事件

在这里插入图片描述

mouseenter测试:
由图看出:只有当光标从外界进入ul的时候才会触发mouseenter事件,进入li不会触发mouseenter

在这里插入图片描述
因此我们可以得出如下结论:
mouseover:当鼠标移入元素或子元素都会触发事件。

mouseenter:当鼠标移入元素才会触发事件。

二、事件是否支持冒泡?
官方文档的一段话:

Unlike the onmouseover event, the onmouseenter event does not bubble.

从官方文档可以看出:mouseenter事件不支持冒泡!!而mouseover时间支持冒泡
所以:光标在进入或离开元素的子元素的时候,会触发mouseover和mouseout事件,但不会触发mouseenter和mouseleave事件!

以上就是mouseover和mouseenter的异同了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值