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:
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>
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.
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>
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!