Hello ~ 可爱的小伙伴我们又见面啦。
下面是我和大家分享的JS隐藏元素的5种方法,希望可以帮助到有需要的小伙伴。
让我们一起来看看吧 GO!

第一种

<!DOCTYPE html>
<html lang="en">
<script src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐藏元素</title>
    <style>
        #myBtn {
            width: 300px;
            height: 150px;
            background-color: aquamarine;
            color: coral;
            font-size: 35px;
        }

        .a {
            display: none;
        }
    </style>
</head>

<body>

    <button id="myBtn">点我有惊喜哦!</button>

    <script>
        var btn = document.getElementById("myBtn")
        btn.addEventListener('click', function (event) {
            document.getElementById("myBtn").classList.add("a");
        })
    </script>
</body>

</html>

第二种

<!DOCTYPE html>
<html lang="en">
<script src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<style>
    #myBtn {
        width: 300px;
        height: 150px;
        background-color: rgb(16, 130, 150);
        color: coral;
        font-size: 35px;
    }
</style>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐藏元素</title>
</head>

<body>

    <button id="myBtn">点我有惊喜哦!</button>

    <script>
        var btn = document.getElementById("myBtn")
        btn.addEventListener('click', function (event) {
            document.getElementById("myBtn").style.display = "none";
        })
    </script>
</body>

</html>

第三种

<!DOCTYPE html>
<html lang="en">
<script src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐藏元素</title>
    <style>
        #myBtn {
            width: 300px;
            height: 150px;
            background-color: rgb(220, 140, 228);
            color: coral;
            font-size: 35px;
        }
    </style>
</head>

<body>

    <button id="myBtn">点我有惊喜哦!</button>

    <script>
        var btn = document.getElementById("myBtn")
        btn.addEventListener('click', function (event) {
            document.getElementById("myBtn").style.visibility = "hidden";
        })
    </script>
</body>

</html>

第四种

<!DOCTYPE html>
<html lang="en">
<script src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐藏元素</title>
    <style>
        #myBtn {
            width: 300px;
            height: 150px;
            background-color: rgb(192, 231, 128);
            color: coral;
            font-size: 35px;
        }
    </style>
</head>

<body>

    <button id="myBtn">点我有惊喜哦!</button>

    <script>
        var btn = document.getElementById("myBtn")
        btn.addEventListener('click', function (event) {
            document.getElementById("myBtn").style.opacity = 0;
        })
    </script>
</body>

</html>

第五种

<!DOCTYPE html>
<html lang="en">
<script src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>隐藏元素</title>
    <style>
        #myBtn {
            width: 300px;
            height: 150px;
            background-color: rgb(156, 97, 121);
            color: coral;
            font-size: 35px;
        }
    </style>
</head>

<body>

    <button id="myBtn">点我有惊喜哦!</button>

    <script>
        var btn = document.getElementById("myBtn")
        btn.addEventListener('click', function (event) {
            $("#myBtn").hide();
        })
    </script>
</body>

</html>

end~

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐