toggle(switch)方法根据传递的参数切换显示匹配元素集的每一个。如果true参数显示所有元素,则false隐藏所有元素。
selector.toggle( switch );
这是此方法使用的所有参数的描述-
switch - 用于打开显示的开关。
以下是一个简单的示例,简单说明了此方法的用法-
<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>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)