本文實例講述了jQuery實現基本隱藏與顯示效果的方法。分享給大家供大家參考,具體如下:
jQuery 隱藏/顯示
語法:
$(selector).hide(speed,callback);$(selector).show(speed,callback);
eg1:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("p").click(function(){$(this).hide();});});</script></head><body><p>田XX,我愛你。</p><p>傻X,田XX。</p><p>滾X,田XX。</p></body><html>運行結果:

eg2:
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script type="text/javascript">$(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>我愛你</h3><div class="ex"><button class="hide" type="button">隱藏</button><p>姓名:田XX <br />田大大<br />田傻逼</p></div></body><html>運行結果:

jQuery toggle()
使用toggle()方法來切換hide()和show()方法。
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").toggle(); });});</script></head><body><button type="button">切換</button><p>這是個段落</p></body><html>運行結果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。
更多關于jQuery相關內容還可查看本站專題:《jQuery切換特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常用插件及用法總結》、《jQuery拖拽特效與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答