javascript怎麼隱藏元素

2022-02-07 13:00:07

javascript隱藏元素的方法:1、使用「元素物件.style.display="none"」語句;2、使用「元素物件.style.visibility="hidden"」語句;3、使用「元素物件.style.opacity=0」語句。

本教學操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。

javascript隱藏元素的幾種方法

第一種

<!DOCTYPE html>
<html>
<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>
<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>
<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>
<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~

【相關推薦:

以上就是javascript怎麼隱藏元素的詳細內容,更多請關注TW511.COM其它相關文章!