当前位置:Gxlcms > 前端框架 > JavaScript隐藏元素的方法有哪些

JavaScript隐藏元素的方法有哪些

时间:2021-07-01 10:21:17 帮助过:28人阅读

方法:1、“元素对象.style.display="none"”语句;2、“对象.style.visibility="hidden"”语句;3、“对象.style.opacity=0”语句;4、“$(selector).hide()”语句。

本教程操作环境:windows7系统、javascript1.8.5&&jquery1.10.0版、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>

【推荐学习:javascript高级教程】

以上就是JavaScript隐藏元素的方法有哪些的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行