码迷,mamicode.com
首页 > Web开发 > 详细

JS递归

时间:2017-08-15 10:21:55      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:res   span   target   分享   ref   cti   open   src   href   

普通写法:

技术分享
   var twoLevel = data[‘result‘];
                            //console.log(twoLevel.length);   4
                            var html = "";
                            for (var i = 0; i < twoLevel.length; i++) {
                                
                                var threeLevel = twoLevel[i].children;
                                if (threeLevel.length > 0) {
                                    html += "<li><a class=‘inactive‘>" + twoLevel[i].displayName + "</a>";
                                    html += "<ul style=‘display: none‘>";
                                    for (var j = 0; j < threeLevel.length; j++) {
                                        var fourLevel = threeLevel[j].children;
                                        if (fourLevel.length > 0) {
                                            html += "<li><a class=‘inactive‘>" + threeLevel[j].displayName + "</a>";
                                            html += "<ul style=‘display: none‘>";
                                            for (var k = 0; k < fourLevel.length; k++) {


                                                var fiveLevel = fourLevel[k].children;
                                                if (fiveLevel.length > 0) {
                                                    html += "<li><a class=‘inactive‘>" + fourLevel[k].displayName + "</a>";
                                                    html += "<ul style=‘display: none‘>";
                                                    for (var w = 0; w < fourLevel.length; w++) {
                                                        html += "<li><a target=‘iframe‘ href=" + fiveLevel[w].url + ">" + fiveLevel[w].displayName + "</a>";
                                                    }
                                                    html += "</ul>";
                                                } else {
                                                    html += "<li><a  target=‘iframe‘ href=‘" + fourLevel[k].url + "‘>" + fourLevel[k].displayName + "</a>";
                                                }

                                                html += "</li>";

                                              
                                            }
                                            html += "</ul>";
                                        } else {
                                            html += "<li><a  target=‘iframe‘ href=‘" + threeLevel[j].url + "‘>" + threeLevel[j].displayName + "</a>";
                                        }
                                        
                                        html += "</li>";

                                    }
                                    html += "</ul>";
                                }
                                else {
                                    html += "<li><a  target=‘iframe‘ href=‘" + twoLevel[i].url + "‘>" + twoLevel[i].displayName + "</a>";
                                }
                                html+="</li>";
                            }
View Code

递归写法:

技术分享
var twoLevel = data[‘result‘];
                            //console.log(twoLevel.length);   4
                            var html = "";
                            function test(childMenu) {
                                for (var i = 0; i < childMenu.length; i++) {

                                    var childData = childMenu[i].children;
                                    if (childData.length > 0) {
                                        
                                        html += "<li><a class=‘inactive‘>" + childMenu[i].displayName + "</a>";
                                        html += "<ul style=‘display: none‘>";
                                        test(childData);
                                        html += "</ul>";

                                    }
                                    else {
                                        html += "<li><a  target=‘iframe‘ href=‘" + childMenu[i].url + "‘>" + childMenu[i].displayName + "</a>";
                                    }
                                    
                                    html += "</li>";
                                }
                            }
                            test(twoLevel);
View Code

 

JS递归

标签:res   span   target   分享   ref   cti   open   src   href   

原文地址:http://www.cnblogs.com/AmbiguousMiao/p/7361845.html

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