📜  jquery div show - Javascript (1)

📅  最后修改于: 2023-12-03 15:02:09.600000             🧑  作者: Mango

jQuery Div Show

jQuery Div Show is a useful feature for web developers who want to show or hide certain div elements on their web pages using jQuery. In this tutorial, we will learn how to use jQuery Div Show to show or hide div elements on a web page dynamically.

Table of Contents
Introduction to jQuery Div Show

jQuery Div Show is a method to show or hide a div element on a web page dynamically. The jQuery function can be used to select the div element and then the show() or hide() method can be used to show or hide the element respectively.

The syntax for jQuery Div Show is as follows:

$(selector).show();
$(selector).hide();
Examples of jQuery Div Show

Let's now look at some examples of jQuery Div Show.

Example 1: Basic jQuery Div Show

In this example, we will show you how to use jQuery Div Show to show or hide a single div element on a web page.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
      $(document).ready(function() {
        $("#show_button").click(function() {
            $("#div_element").show();
        });
        $("#hide_button").click(function() {
            $("#div_element").hide();
        });
      });
    </script>
  </head>

  <body>
    <button id="show_button">Show Element</button>
    <button id="hide_button">Hide Element</button>

    <div id="div_element" style="display:none;">
      <p>Hello World!</p>
    </div>
  </body>
</html>

In the above example, we first include the jQuery library in our document. Then, we use the $(document).ready() function to ensure that our jQuery code is executed only after the document is ready.

We then use the click() function to bind our event listeners to the buttons on our web page. The first click handler shows the div element with id="div_element". The second click handler hides the same element.

Example 2: Multiple jQuery Div Show

In this example, we will show you how to use jQuery Div Show to show or hide multiple div elements on a web page.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
      $(document).ready(function() {
        $("#show_button").click(function() {
            $(".div_element").show();
        });
        $("#hide_button").click(function() {
            $(".div_element").hide();
        });
      });
    </script>
  </head>

  <body>
    <button id="show_button">Show Elements</button>
    <button id="hide_button">Hide Elements</button>

    <div class="div_element" style="display:none;">
      <p>Element 1</p>
    </div>
    <div class="div_element" style="display:none;">
      <p>Element 2</p>
    </div>
    <div class="div_element" style="display:none;">
      <p>Element 3</p>
    </div>
  </body>
</html>

In the above example, we first include the jQuery library in our document. Then, we use the $(document).ready() function to ensure that our jQuery code is executed only after the document is ready.

We then use the click() function to bind our event listeners to the buttons on our web page. The first click handler shows all div elements with class="div_element". The second click handler hides all elements with the same class.

Conclusion

In conclusion, jQuery Div Show is a powerful feature that allows web developers to show or hide div elements on a web page dynamically. We have covered the basics of jQuery Div Show in this tutorial, along with some examples to help you get started.