jQuery& PHP:如何按类重新排序列表项?

时间:2013-11-20 18:47:35

标签: php jquery list smarty reorderlist

首先,抱歉我的英语不好,我希望我能很好地解释我的问题......

所以,我正在努力学习开发,实际上我有这个代码:

<ul>
    <li class="odd ajax_block_product {if $smarty.foreach.myhomeFeaturedProducts.first}first_item{elseif $smarty.foreach.myhomeFeaturedProducts.last}last_item{else}item{/if} {if $smarty.foreach.myhomeFeaturedProducts.iteration%$nbItemsPerLine == 0}last_item_of_line{elseif $smarty.foreach.myhomeFeaturedProducts.iteration%$nbItemsPerLine == 1} {/if} {if $smarty.foreach.myhomeFeaturedProducts.iteration > ($smarty.foreach.myhomeFeaturedProducts.total - $totModulo)}last_line{/if}">...</li>    
    <li class="even ajax_block_category {if $smarty.foreach.myhomeFeaturedCategories.first}first_item{elseif $smarty.foreach.myhomeFeaturedCategories.last}last_item{else}item{/if}">...</li>
</ul>

UL使用smarty自动填充,所以最后我的HTML是这样的:

<ul>
    <li class="odd">1</li>     
    <li class="odd">2</li>
    <li class="odd">3</li>     
    <li class="odd">4</li>

    <li class="even">5</li>     
    <li class="even">6</li>
    <li class="even">7</li>     
    <li class="even">8</li>
</ul>

我的问题是我想用jQuery或PHP替换EVEN和ODD类,但我没有找到。 我想这样做:

     <ul>
        <li class="odd">1</li>     
        <li class="even">5</li>
        <li class="odd">2</li>     
        <li class="even">6</li>

        <li class="odd">3</li>     
        <li class="even">7</li>
        <li class="odd">4</li>     
        <li class="even">8</li>
    </ul>

有人可以告诉我我该怎么做吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

您的代码似乎有点复杂:

$(function () {
    var $items = $('li');
    var odd = $items.filter('.odd').get();
    var even = $items.filter(':not(.odd)').get();
    var mixed = [];
    while (odd.length) {
        mixed.push(odd.shift());
        if (even.length) {
            mixed.push(even.shift());
        }
    }
    if (even.length) {
        mixed = mixed.concat(even);
    }
    $('ul').append(mixed);
});

以下是演示:http://jsfiddle.net/wared/unzxv/

答案 1 :(得分:0)

尝试使用CSS :nth-child伪类,如下所示

li:nth-child(even) { color: green; }
li:nth-child(odd) { color: blue; }

http://www.w3.org/Style/Examples/007/evenodd.en.html

DEMO:http://jsfiddle.net/UCBGJ/

答案 2 :(得分:0)

为什么不只使用smarty的cycle

 <li class="{cycle values="odd,even"} ajax_block_product....

答案 3 :(得分:0)

我发现如何做到这一点。谢谢大家的帮助!以下是我所处情况的人的代码:

$(function () {
    var $oddArray = new Array();
    var $evenArray = new Array();
    $('ul#shuffle li').each(function () {
        if ($(this).hasClass('product')) {
            $oddArray.push($(this).html());
        } else {
            $evenArray.push($(this).html());
        }
    });
    var newLi = new Array();
    var index;
    var oddIndex = 0;
    var evenIndex = 0;
    for (index = 0; index < ($oddArray.length + $evenArray.length); index++) {
        if (index % 2 == 0) {
            console.log("ODD");
            if ($oddArray[oddIndex] != undefined)
                newLi += '<li>' + $oddArray[oddIndex] + '</li>';
            oddIndex++;
        } else {
            console.log("EVEN");
            if ($evenArray[evenIndex] != undefined)
                newLi += '<li>' + $evenArray[evenIndex] + '</li>';
            evenIndex++;
        }
    }
    delete $oddArray;
    delete $evenArray;
    $('ul#shuffle').html(newLi);
});