生成子菜单时,嵌套循环不会附加

时间:2014-07-23 08:24:20

标签: jquery loops

我在代码审核时问了一个问题here,但似乎无法将答案整合到我的代码中。

下拉列表的第一级工作正常,因此它会按预期显示发布者,但使用此代码时,作者不会附加,也不确定原因。

以下是相关代码段:

if (typeof app.cache.publisher[publisherval].author[authorval] == 'undefined') {
app.cache.publisher[publisherval].author[authorval] = $('<li class="author" data-value="' + authorval + '"><a href="#" class="nextmenu">' + authorval + '</a><ul class="sub-menu" data-title="authors"></ul></li>').children('ul').appendTo(ulauthors);
}

这里是完整的代码:

function createDropDown() {
        var target = $('#dropdownMenu');
        for (var i = 0; i < data.genres.length; i++) {
                var genreval = data.genres[i].genre;
                for (var j = 0; j < data.genres[i].publishers.length; j++) {
                        var publisherval = data.genres[i].publishers[j].publisher;
                        if (typeof app.cache.publisher[publisherval] == 'undefined') {
                                target.append('<li class="publisher" data-value="' + publisherval + '"><a href="#" class="nextmenu">' + publisherval + '</a><ul class="sub-menu" data-title="publishers"></ul></li>');
                                app.cache.publisher[publisherval] = {
                                        'ul': target.find('>.publisher[data-value="' + publisherval + '"]>.sub-menu'),
                                        'author': {}
                                };
                        }
                        var ulauthors = app.cache.publisher[publisherval].ul;
                        for (var k = 0; k < data.genres[i].publishers[j].authors.length; k++) {
                                var authorval = data.genres[i].publishers[j].authors[k].author + ' (' + genreval + ')';
                                var author_val = data.genres[i].publishers[j].authors[k].author;
                                var originalgenre = data.genres[i].publishers[j].authors[k].originalgenre;
                                if (typeof app.cache.publisher[publisherval].author[authorval] == 'undefined') {
                                        ulauthors.append('<li class="author" data-value="' + authorval + '"><a href="#" class="nextmenu">' + authorval + '</a><ul class="sub-menu" data-title="authors"></ul></li>');
                                        app.cache.publisher[publisherval].author[authorval] = ulauthors.find('>.author[data-value="' + authorval + '"]>.sub-menu');
                                }
                                var ulyears = app.cache.publisher[publisherval].author[authorval];
                                var gItems = "";
                                for (var m = 0; m < data.genres[i].publishers[j].authors[k].authorYears.length; m++) {
                                        var yearval = data.genres[i].publishers[j].authors[k].authorYears[m];
                                        var year = ulyears.find('.year[data-value="' + yearval + '"]');
                                        if (year.size() == 0) {
                                                var id = ++count;
                                                gItems += '<li class="year" data-value="' + yearval + '"><a id="selyear' + id + '" class="addItem" data-id="' + id + '" data-originalgenre ="' + originalgenre + '"data-year="' + yearval + '" data-publisher="' + publisherval + '" data-author="' + author_val '">' + yearval + '</a></li>';
                                        }
                                }
                                ulyears.append(gItems);
                        };
                };
        };
}

HTML非常基础,只是:

<ul id="dropdownMenu" class="authordropdown">                                      </ul>

通过JS填充。

0 个答案:

没有答案