阅读 132

JS-onmouseover和onmouseenter

2021-07-20_204938.png

在没出父div的情况下,对着子div十进十出,打印结果如下

mouseover:

  1. div1,
  2. div2、div1,
  3. div1,
  4. div2、div1

子div先触发,冒泡的意思吧

mouseenter:

  • div2
  • div2
  • div2

div1不会触发

测试代码

<!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>

    <style>
        .div1 {
            width: 200px;
            height: 200px;
            background-color: aqua;
            position: relative;
            left: 0;
        }

        .div2 {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 200px;
        }
    </style>
</head>

<body>

    <div class="div1">
        <div class="div2"></div>
    </div>

    <script>
        const div1 = document.getElementsByClassName("div1")[0]
        const div2 = document.getElementsByClassName("div2")[0]
        div1.addEventListener("mouseenter", function () {
            console.log("div1")
        })
        div2.addEventListener("mouseenter", function () {
            console.log("div2")
        })
    </script>

</body>

</html>
复制代码
文章分类
前端
文章标签