如何在jQuery中使用jQuery.toggle()方法?

toggle()方法切换显示匹配元素集的每一个。

示例

您可以尝试运行以下代码来学习如何使用jQuery.toggle()方法:

<html>

   <head>
      <title>jQuery toggle() method</title>
      <script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
   
         $(document).ready(function() {

            $("#toggle").click(function(){
               $(".target").toggle( );
            });
         });
           
      </script>
       
      <style>
         p {
             background-color:#bca;
             width:200px;
             border:1px solid green;
         }
      </style>
   </head>
   
   <body>

      <p>Click on the following button:</p>
      <button id = "toggle"> Toggle </button>

      <div class = "target">
         <img src = "../images/jquery.jpg" alt = "jQuery" />
      </div>
 
   </body>
</html>