0

我的脚本中有一部分循环遍历列表中的元素数量,并获取信息然后将其放入不同的 div 中。由于某种原因,第一个 each 循环不会计算其索引。我尝试了几种不同的方法来做到这一点,但似乎都没有奏效。

这是脚本:

//check each Item for a List
if ($(obj).has('> ul')) {

    // Second Level Lists
    $(obj).find('> ul').each(function (index, obj) {

        // Create Second Level Divs
        baseElement.append(
            '<div class="level" id=' + index +
            ' style="color:' + settings.startColor + ';background:' +
            settings.startBg +';font-size:' + settings.baseFont +
            'px;position:absolute;width:100%;height:100%;"></div>'
        );

        // Second Level List Items
        $(obj).find('> li').each(function (i, obj) {
            $('.level#2').append(
                "<span>" +
                $(obj).find('>:first-child').text() +
                "</span>"
            );
        });
    });

}

我需要让它计数,并将每个创建的 div 命名为从上一个开始的数字......我已经拿起了我的大脑,不知道可能出了什么问题。

4

1 回答 1

3

您的内部循环仅附加到.level#2,这可能不是您想要的。如果我正确阅读了您的问题,则应该可以:

$(obj).children('ul').each(function(index) {
    var $parent = $('<div>', {
        'class': 'level',
        id: index,
        css: {
            color: settings.startColor,
            background: settings.startBg,
            fontSize: settings.baseFont + 'px',
            position: 'absolute',
            width: '100%',
            height: '100%'
        }
    }).appendTo(baseElement);

    $(this).children('li').each(function() {
        $('<span>', {
            text: $(this).children().first().text()
        }).appendTo($parent);
    });
});
于 2013-06-22T19:31:53.843 回答