当前位置: 首页 > 文档资料 > jQuery 入门教程 >

toggle(switch )

优质
小牛编辑
113浏览
2023-12-01

描述 (Description)

toggle( switch )方法切换基于传递的参数显示每组匹配的元素。 如果true参数显示所有元素,则false隐藏所有元素。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.toggle( switch );

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • switch - 用于切换显示的开关。

例子 (Example)

以下是一个简单的例子,简单地显示了这种方法的用法 -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#false").click(function(){
               $(".target").toggle(false);
            });
            $("#true").click(function(){
               $(".target").toggle(true);
            });
         });
      </script>
      <style>
         p {background-color:#bca; width:250px; border:1px solid green;}
      </style>
   </head>
   <body>
      <p>Click on any of the following buttons:</p>
      <button id = "false"> False Switch </button>
      <button id = "true"> True Switch </button>
      <div class = "target">
         <img src = "../images/jquery.jpg" alt = "jQuery" />
      </div>
   </body>
</html>