编程学习资讯网

jq 实现的简易树形菜单--编程学习网

发布时间:2018-03-15 09:07:18   来源:本站编辑   浏览次数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery-3.2.1.js"></script>
    <style>
        .tree {
 
        }
        .tree ul li{
            list-style-type:none;
        }
        .tree a{
            cursor:pointer;
        }
        .tree span{
            cursor:pointer;
        }
    </style>
</head>
<body>
    <div id='treedemo' class='tree'>
        
    </div>
    <script>
    var data = [
        {
            id:1,
            city:'云南省',
            child:[
                {
                    id:2,
                    city:'昆明市',
                    child:[
                        {id:3,city:'五华区',child:[]},
                        {id:4,city:'盘龙区',child:[]},
                        {id:5,city:'官渡区',child:[]},
                    ]                              
                },
                {
                    id:6,
                    city:'曲靖市',
                    child:[
                        {id:7,city:'麒麟区',child:[
                            {id:16,city:'开发区',child:[]},
                            {id:17,city:'麒麟花园',child:[]},
                        ]},
                        {id:8,city:'马龙县',child:[]},
                    ]                              
                },
            ]
        },
        {
            id:9,
            city:'贵州省',
            child:[
                {
                    id:10,
                    city:'贵阳市',
                    child:[
                        {id:11,city:'南明区',child:[]},
                        {id:12,city:'云岩区',child:[]},
                    ]
                },
                {
                    id:13,
                    city:'六盘水',
                    child:[
                        {id:14,city:'钟山区',child:[]},
                        {id:15,city:'六枝特区',child:[]},
                    ]
                },
                 
            ]
        }
    ];
      
    function createTree(data){
        var content = '<ul>';
        for(var i=0; i<data.length;i++){
            content += '<li><span>+ </span><a data-id="' + data[i].id + '">'+ data[i].city + '</a>';
            if(data[i].child.length>0){
                content += createTree(data[i].child);
            }
            content += '</li>';
        
        content += '</ul>';
        return content;
    }
    $('#treedemo').html(createTree(data));
    $('#treedemo').on('click',function(e){
        var targetNode = $(e.target);
        var nodeName = targetNode.get(0).tagName.toLowerCase();
        if(nodeName == "span"){
            $(e.target).parent().children("ul").toggle();
        }
        if(nodeName == "a"){
            console.log($(e.target).data('id'));
        }
    });
    </script>
</body>
</html>

 

编程学习网 http://www.javalearns.cn

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java