jQuery 隱藏和顯示
jQuery hide() 和show()
透過jQuery,您可以使用hide() 和show() 方法來隱藏和顯示HTML 元素:
#實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.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>
執(zhí)行程式嘗試
#語法:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
可選的speed 參數(shù)規(guī)定隱藏/顯示的速度,可以取以下值:"slow(慢)"、"fast(快)" 或毫秒。
可選的 callback 參數(shù)是隱藏或顯示完成後所執(zhí)行的函數(shù)名稱。
下面的範例示範了帶有speed 參數(shù)的hide() 方法:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(1000); }); }); </script> </head> <body> <button>隱藏</button> <p>生活就是做出選擇,一旦你做出了你的選擇,你就必須活在你的決定中。</p> </body> </html>
執(zhí)行程式嘗試
jQuery toggle( )
透過jQuery,您可以使用toggle() 方法來切換hide() 和show() 方法。
顯示被隱藏的元素,並隱藏已顯示的元素:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); </script> </head> <body> <button>隱藏/顯示</button> <p>真正的失敗不是你沒有做成事,而是你甘心于失敗。</p> <p>一切都會好起來的,即使不是在今天,總有一天會的。</p> </body> </html>
執(zhí)行程式嘗試
##語法:
$(selector).toggle(speed,callback);可選的speed 參數(shù)規(guī)定隱藏/顯示的速度,可以取下列值: "slow"、"fast" 或毫秒。 可選的 callback 參數(shù)是 toggle() 方法完成後所執(zhí)行的函數(shù)名稱。