本實例實現了樹形的動態菜單,兼容IE8,火狐,Chrome等瀏覽器。使用了jQuery的toggle() 方法。效果和代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>jquery的樹形菜單代碼 </title><meta name="keywords" content="www.cnblogs.com/jihua"/><style type="text/css">body { font-family:"宋體"; font-size: 12px; line-height: 1.5em; color:#7FB0C8; padding:0; margin:0; background: #336699;}ul,ol,li,dl,dt,dd { margin:0; padding:0; list-style-type:none;}h1,h2,h3,form,input,iframe,span { margin:0; padding:0;} a { color:#7FB0C8;}a:link {color: #7FB0C8; TEXT-DECORATION: none;}a:visited {color: #7FB0C8; TEXT-DECORATION: none;}a:hover {color: #fff; TEXT-DECORATION: none;}.white { color:#fff;}.white a:link {color: #fff; TEXT-DECORATION: none;}.white a:visited {color: #fff; TEXT-DECORATION: none;}.white a:hover {color: #73E1F5; TEXT-DECORATION: none;}/* 樹形菜單開始 */.close { float:right; clear:right; font-size:12px; font-weight:normal; cursor:pointer; padding-right:10px;}.title { font-size:14px; color:#fff; margin-bottom:10px; padding-left:5px; width:290px;}.menu { width:290px; height:330px; margin-bottom:10px;}.l1 { background:#000; font-size:13px; padding:5px 0 0 30px; height:20px; margin-bottom:5px; cursor:pointer;}.slist { margin:0 0 5px 0; display:none;}.l2 { padding:0 0 0 35px; font-size:13px;}.l2 a { padding:6px 0 0 5px; width:230px; height:21px; display:block;} .currentl2 a,.l2 a:hover { background:#1E5A82; color:#fff;}.sslist { background:#156890; width:235px; overflow:hidden; margin:0 0 5px 35px; display:none;}.l3 a { padding:6px 0 0 5px; width:230px; height:20px; display:block;} .currentl3 a,.l3 a:hover { color:#fff; font-weight:bold;}</style><script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.4.2.min.js"></script><script type="text/javascript"> // 樹狀菜單 $(document).ready(function () { $(".l1").toggle(function () { $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "slow"); $(this).next(".slist").animate({ height: 'toggle', opacity: 'toggle' }, "slow"); }, function () { $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "slow"); $(this).next(".slist").animate({ height: 'toggle', opacity: 'toggle' }, "slow"); }); $(".l2").toggle(function () { $(this).next(".sslist").animate({ height: 'toggle', opacity: 'toggle' }, "slow"); }, function () { $(this).next(".sslist").animate({ height: 'toggle', opacity: 'toggle' }, "slow"); }); $(".l2").click(function () { $(".l3").removeClass("currentl3"); $(".l2").removeClass("currentl2"); $(this).addClass("currentl2"); }); $(".l3").click(function () { $(".l3").removeClass("currentl3"); $(this).addClass("currentl3"); }); $(".close").toggle(function () { $(".slist").animate({ height: 'toggle', opacity: 'show' }, "fast"); $(".sslist").animate({ height: 'toggle', opacity: 'show' }, "fast"); }, function () { $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "fast"); $(".sslist").animate({ height: 'toggle', opacity: 'hide' }, "fast"); }); });</script></head><body><h1 class="title"><span class="close">全部收起/展開</span>Jihua樹形菜單</h1><div class="menu"> <h1 class="l1">一級菜單</h1> <div class="slist"> <h2 class="l2"><a href="#">二級菜單</a></h2> <ul class="sslist"> <li class="l3"><a href="#">?三級菜單</a></li> <li class="l3"><a href="#">?三級菜單</a></li> <li class="l3"><a href="#" target="_blank">?jihua.cnblogs.com</a></li> <li class="l3"><a href="#">?三級菜單</a></li> </ul> <h2 class="l2"><a href="#">二級菜單</a></h2> <ul class="sslist"> <li class="l3"><a href="#">?三級菜單</a></li> <li class="l3"><a href="#">?三級菜單</a></li> <li class="l3"><a href="#" target="_blank">?三級菜單</a></li> <li class="l3"><a href="#">?三級菜單</a></li> </ul> <h2 class="l2"><a href="#">二級VeVB.COm</a></h2> </div> <h1 class="l1">一級腳本</h1> <div class="slist"> <h2 class="l2"><a href="#">二級菜單計劃</a></h2> <h2 class="l2"><a href="#">二級菜單</a></h2> <h2 class="l2"><a href="#">二級菜單</a></h2> </div> <h1 class="l1">一級菜單</h1> <div class="slist"> <h2 class="l2"><a href="#">二級菜單</a></h2> <h2 class="l2"><a href="#">二級菜單</a></h2> <h2 class="l2"><a href="#">二級菜單</a></h2> </div></div></body></html>
本實例用到jquery的toggle() 方法,介紹如下:
定義和用法
toggle() 方法切換元素的可見狀態。
如果被選元素可見,則隱藏這些元素,如果被選元素隱藏,則顯示這些元素。
語法
$(selector).toggle(speed,callback,switch)
提示和注釋
注釋:該效果適用于通過 jQuery 隱藏的元素,或在 CSS 中聲明 display:none 的元素(但不適用于 visibility:hidden 的元素)。
以上就是jquery實現樹形菜單完整代碼,希望對大家的學習jquery程序設計有所幫助。
新聞熱點
疑難解答