Home > Web Front-end > Front-end Q&A > jquery hides and displays dynamic divs

jquery hides and displays dynamic divs

WBOY
Release: 2023-05-18 20:50:06
Original
1017 people have browsed it

jQuery is a fast, concise, and powerful Javascript library that provides a wealth of tools and functions for web development. In web development, jQuery can be used to achieve various effects to make the website more beautiful and easier to use. Among them, hiding and showing dynamic div elements is a common requirement. This article will introduce how to use jQuery to achieve this effect.

Hide and show dynamic div elements are actually changing the CSS properties of the element. Specifically, when hiding a div element, we need to set its display attribute to none, so that the element will not be displayed; when displaying a div element, we need to set its display attribute to block or other values, so that The element will redisplay.

Using jQuery, we can use the following two methods to hide and show dynamic div elements:

  1. Use the hide() and show() methods

Using jQuery's hide() and show() methods is the simplest and most direct way. These two methods can hide and show elements directly without setting any CSS properties of the element.

The following is a code example using the hide() and show() methods:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Hide and Show Demo</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .dynamic-div {
            width: 200px;
            height: 100px;
            background-color: #ccc;
            margin: 50px auto;
            text-align: center;
            line-height: 100px;
            font-size: 24px;
            display: none; /* 默认隐藏 */
        }
    </style>
</head>
<body>
    <button id="hide-btn">Hide</button>
    <button id="show-btn">Show</button>
    <div class="dynamic-div">This is a dynamic div element.</div>
    <script>
        $(document).ready(function() {
            $("#hide-btn").click(function() {
                $(".dynamic-div").hide();
            });
            $("#show-btn").click(function() {
                $(".dynamic-div").show();
            });
        });
    </script>
</body>
</html>
Copy after login

In the above code, we define a div element with class dynamic-div and set it The display attribute is set to none, which means the element is hidden by default. Then, two buttons are added to the page, using the hide() and show() methods to hide and show the element respectively. When the Hide button is clicked, the element will be hidden, and when the Show button is clicked, the element will be redisplayed.

  1. Use the css() method

In addition to using the hide() and show() methods, we can also use the css() method to modify the CSS of dynamic div elements Attributes. Specifically, we can use the css() method to modify the display attribute to achieve the effect of hiding and showing elements.

The following is a code example using the css() method:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Hide and Show Demo</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .dynamic-div {
            width: 200px;
            height: 100px;
            background-color: #ccc;
            margin: 50px auto;
            text-align: center;
            line-height: 100px;
            font-size: 24px;
            display: none; /* 默认隐藏 */
        }
    </style>
</head>
<body>
    <button id="hide-btn">Hide</button>
    <button id="show-btn">Show</button>
    <div class="dynamic-div">This is a dynamic div element.</div>
    <script>
        $(document).ready(function() {
            $("#hide-btn").click(function() {
                $(".dynamic-div").css("display", "none");
            });
            $("#show-btn").click(function() {
                $(".dynamic-div").css("display", "block");
            });
        });
    </script>
</body>
</html>
Copy after login

In the above code, we use the css() method to modify the display attribute of the .dynamic-div element, thereby achieving Effect of hiding and showing elements. When the Hide button is clicked, the element will be hidden, and when the Show button is clicked, the element will be redisplayed.

Summary

It is very simple to use jQuery to hide and show dynamic div elements. We can use the hide() and show() methods to hide and show elements directly, or we can use the css() method to modify the display attribute of the element to achieve the effect of hiding and showing. It should be noted that when there are multiple dynamic div elements in the page, we need to use class or ID to specify the elements that need to be hidden or displayed.

The above is the detailed content of jquery hides and displays dynamic divs. For more information, please follow other related articles on the PHP Chinese website!

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