每隔三个<li> </li> </div>插入<div>

时间:2010-06-16 15:21:08

标签: javascript jquery json

我有这样的事情:

function cat_filter() {
    $.ajax({
        type: "POST",
        url: 'json/cat_filter.aspx',
        data: "catId=" + "&styleId=" + "&colourId=" + "&sizeId=" + "&minPrice=" + "&maxPrice=",
        dataType: "json",
        beforeSend: function () {
            //load loading cursor
        }, success: function (data) {
            var CatItems = "";

            for (var x = 0; x < data.PRODUCTS.length; x++) {
                CatItems += '<li class="jcarousel-item jcarousel-item-horizontal jcarousel-item-' + [x] + ' jcarousel-item-' + [x] + '-horizontal jcarousel-item-placeholder jcarousel-item-placeholder-horizontal"><a class="large_image" href="#"><img  src="' + data.PRODUCTS[x].product_img + '" alt="' + data.PRODUCTS[x].product_name + '"></a><h3 class="geo_17_darkbrown">' + data.PRODUCTS[x].product_name + '</h3>';

                if (data.PRODUCTS[x].product_onsale == 1) {
                    CatItems += '<img alt="sale" src="assets/images/sale.gif" class="sale"><span class="geo_17_red_linethr">&pound;' + data.PRODUCTS[x].product_retailprice + '</span>&nbsp;&nbsp;<span class="price geo_17_darkbrown">&pound;' + data.PRODUCTS[x].product_webprice + '</span>';
                } else {
                    CatItems += '<span class="price geo_17_darkbrown">&pound;' + data.PRODUCTS[x].product_webprice + '</span>';
                }

                if (data.PRODUCTS[x].product_COLOURS) {
                    CatItems += '<span class="colour">';

                    for (var y = 0; y < data.PRODUCTS[x].product_COLOURS.length; y++) {

                        CatItems += '<span><a href="' + data.PRODUCTS[x].product_COLOURS[y].colours_large + '"><img src="' + data.PRODUCTS[x].product_COLOURS[y].colours_thumb + '" alt="' + data.PRODUCTS[x].product_COLOURS[y].colour_name + '" /></a></span>';
                    }

                    CatItems += '</span>';
                }

                CatItems += '</li>';

            }
            $('.carousel_00 ul').html(CatItems);
        }, complete: function () {
            //remove loading cursor
        }
    });
}

此代码生成此html:

    <div class="carousel_00">
        <ul>            
            <li><a href="#" class="large_image"><img  src="assets/images/dress1.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            <span class="colour">
                <span><a href="assets/images/big_image_1.gif"><img src="assets/images/black.gif" alt="balck"></a></span>
                <span><img src="assets/images/brown.gif" alt="brown"></span>
                <span><img src="assets/images/purple.gif" alt="purple"></span>
            </span>
            </li>
            <li><a href="#"><img  src="assets/images/dress2.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><img class="sale" src="assets/images/sale.gif" alt="sale" /><a href="#"><img  src="assets/images/dress3.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="geo_17_red_linethr">&pound;99.99</span>&nbsp;&nbsp;<span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress1.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;59.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress2.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress3.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress1.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress2.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
            <li><a href="#"><img  src="assets/images/dress3.gif" alt="image"></a>
            <h3 class="geo_17_darkbrown">Rachel Dress</h3>
            <span class="price geo_17_darkbrown">&pound;89.99</span>
            </li>
        </ul></div>

我需要的是每3个li将在div / div中。 我知道这不是语义而不是正确,但这只是举例。 (基本上如果我知道如何做到这一点,我将替换li的跨度以及我需要在li之外的那个div)。

如果有人会帮助我,我会很高兴的。因为我拥有的代码对我来说已经太过分了。

2 个答案:

答案 0 :(得分:1)

您可以先在循环中添加:

if (x % 3 === 0) CatItems += '<div>';

这是循环中的最后一个:

if (x % 3 === 2 || x = data.PRODUCTS.length - 1) CatItems += '</div>';

这会将li项目三和树分组。如果您只希望每个组中第一个div周围的li,则第二个条件将与第一个条件相同。

(让我重复一下你在问题上所说的关于这在语义上不正确的问题。)

答案 1 :(得分:0)

不使用字符串,而是使用dom函数(document.createElement和element.appendChild)。但如果我坚持使用你的方法,那就是这样的:

               success: function(data) {
                   var CatItems = "";

                   for(var x=0; x < data.PRODUCTS.length; x++) {
                        if(x%3==0)
                            CatItems += "<div>";
                        CatItems += '<li class="jcarousel-item jcarousel-item-horizontal jcarousel-item-'+[x]+' jcarousel-item-'+[x]+'-horizontal jcarousel-item-placeholder jcarousel-item-placeholder-horizontal"><a class="large_image" href="#"><img  src="'+ data.PRODUCTS[x].product_img +'" alt="' + data.PRODUCTS[x].product_name +'"></a><h3 class="geo_17_darkbrown">' + data.PRODUCTS[x].product_name +'</h3>';

                        if ( data.PRODUCTS[x].product_onsale==1 ) {
                            CatItems += '<img alt="sale" src="assets/images/sale.gif" class="sale"><span class="geo_17_red_linethr">&pound;'+ data.PRODUCTS[x].product_retailprice +'</span>&nbsp;&nbsp;<span class="price geo_17_darkbrown">&pound;'+ data.PRODUCTS[x].product_webprice +'</span>';
                        } else {
                            CatItems += '<span class="price geo_17_darkbrown">&pound;'+ data.PRODUCTS[x].product_webprice +'</span>';
                        }

                        if ( data.PRODUCTS[x].product_COLOURS ) {
                            CatItems += '<span class="colour">';

                            for(var y=0; y < data.PRODUCTS[x].product_COLOURS.length; y++) {

                                CatItems += '<span><a href="'+ data.PRODUCTS[x].product_COLOURS[y].colours_large +'"><img src="'+ data.PRODUCTS[x].product_COLOURS[y].colours_thumb +'" alt="'+ data.PRODUCTS[x].product_COLOURS[y].colour_name +'" /></a></span>';
                            }

                            CatItems += '</span>';
                        }

                        CatItems += '</li>';

                        if(x%3==0)
                            CatItems += "</div>";

                   }
                   $('.carousel_00 ul').html(CatItems);
               },