How to hide elements in javascript

青灯夜游
Release: 2022-02-18 17:21:53
Original
9362 people have browsed it

How to hide elements in javascript: 1. Use the "element object.style.display="none"" statement; 2. Use the "element object.style.visibility="hidden"" statement; 3. Use " Element object.style.opacity=0" statement.

How to hide elements in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Several ways to hide elements in javascript

The first one

<!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(&#39;click&#39;, function (event) {
            document.getElementById("myBtn").style.display = "none";
        })
    </script>
</body>

</html>
Copy after login

The second type

<!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(&#39;click&#39;, function (event) {
            document.getElementById("myBtn").style.visibility = "hidden";
        })
    </script>
</body>

</html>
Copy after login

The third type

<!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(&#39;click&#39;, function (event) {
            document.getElementById("myBtn").style.opacity = 0;
        })
    </script>
</body>

</html>
Copy after login

The fourth type

<!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(&#39;click&#39;, function (event) {
            $("#myBtn").hide();
        })
    </script>
</body>

</html>
Copy after login

end~

【Related recommendations: javascript learning tutorial

The above is the detailed content of How to hide elements in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!