码迷,mamicode.com
首页 > 编程语言 > 详细

[JavaScritp]构建简单的json树形菜单

时间:2014-10-31 13:47:36      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   java   for   

json结构:

bubuko.com,布布扣
var Menu = [{
            tit:"一级菜单",
            submenu:[{
                tit:"二级菜单",
                url:"",
                func:function(){
                    alert(‘what do you want to do?‘);
                }
            },{
                tit:"二级菜单",
                func:function(){
                    alert(‘do what?‘);
                },
                submenu:[{
                    tit:"三级菜单",
                    url:"",
                    submenu:[{
                        tit:"四级菜单",
                        url:""
                    },{
                        tit:"四级菜单",
                        url:""
                    }]
                },{
                    tit:"三级菜单",
                    url:""
                }]
            }]
        },{
            tit:"一级菜单",
            submenu:[{
                tit:"二级菜单",
                url:""
            },{
                tit:"二级菜单",
                submenu:[{
                    tit:"三级菜单",
                    url:""
                },{
                    tit:"三级菜单",
                    url:""
                }]
            }]
        }];
View Code

构建菜单处理函数

bubuko.com,布布扣
/*
     * @构建树形菜单
     * @arrJson:json数据
     * @container:菜单容器
     */
    function menuTree(jsonArr,container,treeId){
        var oText,oUrl;
        var oUl = document.createElement(‘ul‘);
        for(var i = 0 ;i < jsonArr.length; i++){
            var oLi = document.createElement(‘li‘);
            oUrl = jsonArr[i].url || "javascript:void(0)";
            oText = jsonArr[i].tit;
            if(jsonArr[i].submenu){
                 oLi.innerHTML =‘<a href="‘+oUrl+‘">‘+oText+‘</a>‘; 
                 menuTree(jsonArr[i].submenu,oLi);//递归构建子菜单
            }else{
                oLi.innerHTML = ‘<a href="‘+oUrl+‘">‘+oText+‘</a>‘;
            }
            //自定义函数
            if(typeof jsonArr[i].func =="function"){ 
                oLi.getElementsByTagName(‘a‘)[0].func = jsonArr[i].func;
                oLi.getElementsByTagName(‘a‘)[0].onclick=function(){
                    this.func();
                }
            }
            oUl.appendChild(oLi);
        }
        //最外层容器事件委托
        if(treeId){
            document.getElementById(treeId).onclick = function(e){
                var event = e || window.event;
                var target = event.target||event.srcElement;
                var next = target.nextSibling;
                if(target.nodeName.toLowerCase() == "a"){
                    if(next){
                        if(next.style.display=="" || next.style.display=="block"){
                            next.style.display="none";
                        }else{
                            next.style.display="block";
                        }
                    }
                }
            }
        }
        container.appendChild(oUl);
    }
View Code

完整demo:

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json树形菜单</title>
</head>
<body>
    <h2>构建json树形菜单</h2>
    <div class="tree" id="tree"></div>
    <script type="text/javascript">
    window.onload = function(){
        var Menu = [{
            tit:"一级菜单",
            submenu:[{
                tit:"二级菜单",
                url:"",
                func:function(){
                    alert(what do you want to do?);
                }
            },{
                tit:"二级菜单",
                func:function(){
                    alert(do what?);
                },
                submenu:[{
                    tit:"三级菜单",
                    url:"",
                    submenu:[{
                        tit:"四级菜单",
                        url:""
                    },{
                        tit:"四级菜单",
                        url:""
                    }]
                },{
                    tit:"三级菜单",
                    url:""
                }]
            }]
        },{
            tit:"一级菜单",
            submenu:[{
                tit:"二级菜单",
                url:""
            },{
                tit:"二级菜单",
                submenu:[{
                    tit:"三级菜单",
                    url:""
                },{
                    tit:"三级菜单",
                    url:""
                }]
            }]
        }];
    //构建菜单
     menuTree(Menu,document.getElementById(tree),tree);
    }

    /*
     * @构建树形菜单
     * @arrJson:json数据
     * @container:菜单容器
     */
    function menuTree(jsonArr,container,treeId){
        var oText,oUrl;
        var oUl = document.createElement(ul);
        for(var i = 0 ;i < jsonArr.length; i++){
            var oLi = document.createElement(li);
            oUrl = jsonArr[i].url || "javascript:void(0)";
            oText = jsonArr[i].tit;
            if(jsonArr[i].submenu){
                 oLi.innerHTML =<a href="+oUrl+">+oText+</a>; 
                 menuTree(jsonArr[i].submenu,oLi);//递归构建子菜单
            }else{
                oLi.innerHTML = <a href="+oUrl+">+oText+</a>;
            }
            //自定义函数
            if(typeof jsonArr[i].func =="function"){ 
                oLi.getElementsByTagName(a)[0].func = jsonArr[i].func;
                oLi.getElementsByTagName(a)[0].onclick=function(){
                    this.func();
                }
            }
            oUl.appendChild(oLi);
        }
        //最外层容器事件委托
        if(treeId){
            document.getElementById(treeId).onclick = function(e){
                var event = e || window.event;
                var target = event.target||event.srcElement;
                var next = target.nextSibling;
                if(target.nodeName.toLowerCase() == "a"){
                    if(next){
                        if(next.style.display=="" || next.style.display=="block"){
                            next.style.display="none";
                        }else{
                            next.style.display="block";
                        }
                    }
                }
            }
        }
        container.appendChild(oUl);
    }
    </script>
</body>
</html>
View Code

bubuko.com,布布扣

[JavaScritp]构建简单的json树形菜单

标签:style   blog   http   io   color   os   ar   java   for   

原文地址:http://www.cnblogs.com/bigboyLin/p/4064714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!