Use js code to realize clicking the button to hide and display the div frame

Content description: use js code to realize clicking the button to hide and display the div frame, click once to expand the div, click again to hide

code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #div1 {
            width: 100px;
            height: 400px;
            background-color: red;
            display: none;
        }
    </style>
    <script>
        function f() {
            var v1 = document.getElementById("div1");
            if (v1.style.display == "none") {
                v1.style.display = "block";
            } else {
                v1.style.display = "none";
            }
        }
    </script>
</head>
<body>
<input type="button" value="显示隐藏" onclick="f()">
<div id="div1">

</div>
</body>
</html>

Renderings:

initial page

click once

 

click twice

 

 

Guess you like

Origin blog.csdn.net/psjasf1314/article/details/124104332