wuyuchuan
<!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>显示和隐藏图片</title>

    <style>
        .box{
            position: relative;
            width: 100px;
            height: 100px;
            border: 1px solid red;
            margin: 100px auto;
            text-align: center;
        }

        .box img{
            width: 100px;
            height: 100px;
        }

        .close-btn{
            position: absolute;
            bottom: -30px;
            left: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="box">
        <img src="tao.png" alt="">
        <button class="close-btn">点我</button>
    </div>

    <script>
        //获取元素
        var img = document.querySelector(\'img\');
        var btn = document.querySelector(\'.close-btn\');
        
        var flag = 0;
        //注册事件 程序处理
        btn.onclick = function(){
            if(flag == 0){
                img.style.display = "none";
                flag = 1;
            }else{
                img.style.display = "block";
                flag = 0;
            }
        }
    </script>
</body>
</html>

 

分类:

技术点:

相关文章: