我將CSS完全分離出來用jQuery附加式樣,就是為了多級染色,并且生成目錄樹和控制式樣也很容易,生成時也不需要考慮式樣。數(shù)據(jù)表建議用事先Order排序的方式,不要讀取數(shù)據(jù)的時候才分級排序,這樣性能會較佳。 我把它做成了個.Net的控件,作為輕量級的無限目錄樹,還是相當好用的。只是還不完善,我先慢慢修改,等差不多了再發(fā)布出來。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> 轉(zhuǎn)自:http://blog.csdn.net/jpr1990/article/details/7245713
<html xmlns="http://www./1999/xhtml"> <head id="Head1" runat="server"> <title>jQuery 無限級菜單</title> <style type="text/css"> #menu a { color:#fff; } #menu div { /* text-align:center; */ } #menu div a { padding-left:20px; } #menu div.root { display:block; } .list { background:url(list.gif) no-repeat 6px 6px; } </style> <script type="text/javascript" language="javascript" src="jquery-1.2.3.min.js"></script> <script type="text/javascript" language="javascript"> $(function(){ //顏色列表,如果想支持無限級,最好自動生成顏色列表,不過我的配色一項很差,用生成的就更慘不忍睹了…… _cor = ['#003366', '#0066CC', '#3399FF', '#990000', '#CC0000', '#FF3300', '#FF9900', '#FFCC66', '#FFFFFF']; //初始化類 (function Init(i,obj){ i++; //查找子節(jié)點 _obj = obj.children('div'); //若有子節(jié)點,則增加一個專有式樣 if (_obj.length > 0) obj.addClass('list'); $.each(_obj, function(j,o){ //若是子目錄則隱藏 if (i > 0) $(o).hide(); //根據(jù)目錄級數(shù)查找顏色字典上背景色,可改為圖片什么的。 $(o).css('background-color',_cor[i]); //查找子目錄 Init(i,$(o)); }); })(-1,$('#menu')); }); //跳轉(zhuǎn)鏈接 function GotoURL(obj) { //若鏈接最末一位不是符號“#”則跳轉(zhuǎn)鏈接,因為取href得到鏈接絕對路徑,所以只能取最后一位,其實可以傳值判斷或生成目錄樹時不產(chǎn)生onclick都是可以的 if (obj.href.substring(obj.href.length - 1, obj.href.length) != "#") return true; //拉出和縮進的特效 $.each($(obj).parent().children('div'), function(i,o){ $(o).slideToggle('slow'); }); return false; } </script> </head> <body> <div id="menu"> <div> <a href="#" onclick="return GotoURL(this)">第一級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第一級</a> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> <div> <a href="#" onclick="return GotoURL(this)">第三級</a> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> </div> <div> <a href="#" onclick="return GotoURL(this)">第三級</a> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> <div> <a href="#" onclick="return GotoURL(this)">第六級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第六級</a> </div> </div> </div> </div> </div> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> </div> </div> <div> <a href="#" onclick="return GotoURL(this)">第一級</a> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第二級</a> <div> <a href="#" onclick="return GotoURL(this)">第三級</a> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> </div> <div> <a href="#" onclick="return GotoURL(this)">第三級</a> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第四級</a> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第五級</a> <div> <a href="#" onclick="return GotoURL(this)">第六級</a> </div> <div> <a href="#" onclick="return GotoURL(this)">第六級</a> </div> </div> </div> </div> </div> </div> </div> </body> </html> |
|