实例
jQuery hide()
简单的jQuery hide()方法演示。
<!DOCTYPE html> <html> <head> <title>test</title> <script src="jquery-3.1.1.min.js"> </script> <script> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); }); </script> </head> <body> <p>点我,我就会消失。</p> <p>继续点我!</p> <p>接着点我!</p> </body> </html>
jQuery hide()
另一个hide()实例。演示如何隐藏文本。
<!DOCTYPE html> <html> <head> <title>test</title> <script src="jquery-3.1.1.min.js"> </script> <script> $(document).ready(function(){ $(".ex .hide").click(function(){ $(this).parents(".ex").hide("slow"); }); }); </script> <style type="text/css"> div.ex { background-color:#e5eecc; padding:7px; border:solid 1px #c3c3c3; } </style> </head> <body> <h3>问世间Java为何物</h3> <div class="ex"> <button class="hide">点我隐藏</button> <p>问世间Java为何物<br> 站点 URL:http://injavawetrust.iteye.com</p> </div> <h3>直教人生死相许</h3> <div class="ex"> <button class="hide">点我隐藏</button> <p>直教人生死相许<br> 站点 URL:http://injavawetrust.iteye.com</p> </div> </body> </html>
jQuery hide() 和 show()
通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素:
实例
<!DOCTYPE html> <html> <head> <title>test</title> <script src="jquery-3.1.1.min.js"> </script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>如果你点击“隐藏” 按钮,我将会消失。</p> <button id="hide">隐藏</button> <button id="show">显示</button> </body> </html>
语法:
$(selector).hide(speed,callback); $(selector).show(speed,callback);
可选的 speed 参数规定隐藏/显示的速度,可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是隐藏或显示完成后所执行的函数名称。
下面的例子演示了带有 speed 参数的 hide() 方法:
实例
<!DOCTYPE html> <html> <head> <title>test</title> <script src="jquery-3.1.1.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(1000); }); }); </script> </head> <body> <button>隐藏</button> <p>这是个段落,内容比较少。</p> <p>这是另外一个小段落</p> </body> </html>
jQuery toggle()
通过 jQuery,您可以使用 toggle() 方法来切换 hide() 和 show() 方法。
显示被隐藏的元素,并隐藏已显示的元素:
实例
<!DOCTYPE html> <html> <head> <title>test</title> <script src="jquery-3.1.1.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); </script> </head> <body> <button>隐藏/显示</button> <p>这是一个文本段落。</p> <p>这是另外一个文本段落。</p> </body> </html>
语法:
$(selector).toggle(speed,callback);
可选的 speed 参数规定隐藏/显示的速度,可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是 toggle() 方法完成后所执行的函数名称。