toggle()方法可切换显示匹配元素集的每一个。
selector.toggle( );
以下是一个简单的示例,简单说明了此方法的用法-
<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() { $("#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>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)