jQuery 無限級菜單的簡單一實例_jquery

來源:互聯網
上載者:User

我將CSS完全分離出來用jQuery附加式樣,就是為了多級染色,並且組建目錄樹和控制式樣也很容易,產生時也不需要考慮式樣。資料表建議用事先Order排序的方式,不要讀取資料的時候才分級排序,這樣效能會較佳。

我把它做成了個.Net的控制項,作為輕量級的無限分類樹,還是相當好用的。只是還不完善,我先慢慢修改,等差不多了再發布出來。

複製代碼 代碼如下:

<!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 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++;
            //尋找子節點
            _obj = obj.children('div');
            //若有子節點,則增加一個專有式樣
            if (_obj.length > 0)
                obj.addClass('list');
            $.each(_obj, function(j,o){
                //若是子目錄則隱藏
                if (i > 0)
                    $(o).hide();
                //根據目錄級數尋找顏色字典上背景色,可改為圖片什麼的。
                $(o).css('background-color',_cor[i]);
                //尋找子目錄
                Init(i,$(o));
            });
        })(-1,$('#menu'));
    });
    //跳轉連結
    function GotoURL(obj) {
        //若連結最末一位不是符號“#”則跳轉連結,因為取href得到連結絕對路徑,所以只能取最後一位,其實可以傳值判斷或組建目錄樹時不產生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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.