mouseenter 和 mouseover的区别

mouseenter 鼠标事件

  1. 当鼠标移动到元素上时就会触发 mouseenter 事件
  2. 类似 mouseover,它们两者事件的差别是
  3. mouseover 鼠标经过自身盒子会触发,经过子盒子还会触发,mouseenter 只会经过自身盒子触发
  4. 之所以这样,就是因为mouseenter不会冒泡
  5. 跟 mouseenter 搭配鼠标离开 mouseleave 同样不会冒泡

栗子:

<!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>
        .father {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script>
        var father = document.querySelector('.father');
        var son = document.querySelector('son');
        // father.addEventListener('mouseover', function() {
        //     console.log(11);

        // })
        father.addEventListener('mouseenter', function() {
            console.log(11);
        })
    </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章