随缘 我不喜欢太复杂的东西

导航

Javascript树形json生成<ul><li>

        var makeTree = function (parentObj, treeJson) {
            var ulObj = $("<ul></ul>");
            for (var i = 0; i < treeJson.length; i++) {
                var childHtml = "<li>";
                var aHtml = "<a data-id='" + treeJson[i].id + "'>" + treeJson[i].name + "</a>";
                childHtml += aHtml;
                childHtml += "</li>";

                var childObj = $(childHtml);
                if (treeJson[i].children != null && treeJson[i].children.length > 0) {
                    makeTree(childObj, treeJson[i].children);
                }
                $(ulObj).append(childObj);
            }
            $(parentObj).append($(ulObj));
        };

 

posted on 2020-03-23 22:26  随缘py  阅读(502)  评论(0编辑  收藏  举报