两个盒子的移入移出

2022-03-08  本文已影响0人  心存美好

两个盒子的移入移出

<!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>
        body {
            font-family: "Microsoft YaHei", serif;
        }

        body,
        dl,
        dd,
        p,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            margin: 0;
        }

        ol,
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        img {
            border: none;
        }

        #box1,
        #box2 {
            width: 200px;
            height: 50px;
            background: #999;
            margin: 50px auto;
            line-height: 50px;
            font-weight: bolder;
            color: #fff;
            text-align: center;
            font-size: 12px;
        }

        #box1.show {
            background: red;
            color: pink;
        }

        #box2.show {
            background: skyblue;
            color: white;
        }
    </style>
</head>

<body>
    <div id="box1"></div>

    <div id="box2"></div>

    <script>
        // 1. 获取DOM元素
        let oBox1 = document.getElementById('box1')
        let oBox2 = document.getElementById('box2')
        // 2. 绑定鼠标事件
        oBox1.onmouseenter = function () {
            this.innerHTML = '我被移入了'
            // this.style.background='skyblue'//以前加加样式的写法,
            // this.style.color='red'
            oBox2.innerHTML = 'Box1被移入了'
            this.className = 'show'//将样式放在#box1.show选择器里,加类名显示
        }.onmouseleave = function () {
            thi
        oBox1s.innerHTML = ''
            this.className = ''   //类名不显示
            oBox2.innerHTML = ''
        }
        oBox2.onmouseenter = function () {
            this.innerHTML = '我被移入了'
            this.className = 'show'
            oBox1.innerHTML = 'Box1被移出了'
        }
        oBox2.onmouseleave = function () {
            this.innerHTML = ''
            this.className = ''
            oBox1.innerHTML = ''
        }
    </script>
</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读