Home > Web Front-end > JS Tutorial > Use jQuery to dynamically control the display and hiding of elements

Use jQuery to dynamically control the display and hiding of elements

WBOY
Release: 2024-02-25 09:42:37
Original
1229 people have browsed it

Use jQuery to dynamically control the display and hiding of elements

jQuery Tip: Dynamically change the display attribute of an element

In web development, we often encounter the need to dynamically display or hide elements based on user interaction or other conditions. Condition. Among them, changing the display attribute of an element is one of the common and effective methods. By using the jQuery library, we can easily dynamically change the display attributes of elements, making web page interaction more flexible and vivid. This article will introduce how to use jQuery to dynamically change the display attribute of an element and provide specific code examples.

First, we need to introduce the jQuery library into the HTML document. It can be introduced in the following ways:

<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
Copy after login

Next, we will show three common scenarios and give corresponding code examples:

  1. Click the button to show or hide the element:
    In this scenario, we will show or hide an element by clicking a button. Suppose we have a button and a paragraph that needs to be shown or hidden. The code is as follows:
<button id="toggleButton">点击切换显示/隐藏</button>
<p id="targetElement" style="display: none;">这是需要显示或隐藏的元素。</p>
Copy after login

Next, we use jQuery to achieve the effect of switching the paragraph display or hiding when the button is clicked:

$(document).ready(function() {
    $('#toggleButton').click(function() {
        $('#targetElement').toggle();
    });
});
Copy after login

Through the above code, we can achieve the effect of switching the display or hiding of paragraphs when clicking the button.

  1. Show or hide elements based on conditions:
    Sometimes, we need to show or hide elements based on specific conditions. For example, show or hide a section of text based on a checkbox selection. The following is a sample code:
<input type="checkbox" id="checkbox"> 显示/隐藏文字
<p id="conditionalElement" style="display: none;">根据复选框选择显示或隐藏的文字。</p>
Copy after login

Next, we use jQuery to control the display or hiding of text based on the checkbox status:

$(document).ready(function() {
    $('#checkbox').change(function() {
        if($(this).is(':checked')) {
            $('#conditionalElement').show();
        } else {
            $('#conditionalElement').hide();
        }
    });
});
Copy after login

Through the above code, when checked When the box is checked, the text is displayed; when the checkbox is unchecked, the text is hidden.

  1. Fade effect shows or hides elements:
    In addition to directly displaying or hiding elements, we can also use the fade effect to achieve a smoother display or hide transition. The following is a sample code:
<button id="fadeButton">点击淡入/淡出</button>
<p id="fadeElement" style="display: none;">这是通过淡入淡出效果显示和隐藏的元素。</p>
Copy after login

Next, we use jQuery to use the fade effect to show or hide elements when clicking the button:

$(document).ready(function() {
    $('#fadeButton').click(function() {
        $('#fadeElement').fadeToggle();
    });
});
Copy after login

Through the above code, we can achieve Use the fade effect to show or hide elements when the button is clicked.

Summary:
Through the jQuery library, we can easily dynamically change the display attribute of elements to achieve various display or hide effects. In the above example, we demonstrated how to dynamically change the display attribute of an element by clicking a button, based on conditions, fading in and out, etc. I hope this article can help you better master jQuery skills and apply them to actual web development.

The above is the detailed content of Use jQuery to dynamically control the display and hiding of elements. 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