dongshuming7131 2013-11-20 18:47
浏览 33
已采纳

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

Firstly, sorry for my bad english, I hope I'll explain well my problem...

So, I'm trying to learn developping and actually I have this code :

<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>

The UL populates automaticly with smarty, so at the end my HTML is like this :

<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>

My problem is that I want to alternate EVEN and ODD classes with jQuery or PHP, but I didn't find how. I would like to make this :

     <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>

Can somebody tell me how I need to do this please?

Thank you!

  • 写回答

4条回答 默认 最新

  • dua27031 2013-11-22 14:32
    关注

    I find how to do this. Thank you all for helping me ! Here's the code for someone in my situation :

    $(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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀