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