【问题标题】:How to Use JS to sort LI by today's date如何使用JS按今天的日期对LI进行排序
【发布时间】:2014-12-28 10:09:21
【问题描述】:

所以:

我在 Bootstrap 模板上有一个定价表,其中有 7 列,一个用于一周中的每一天(它将用于显示俱乐部的活动)。相当标准。它看起来像这样:

 <ul class="calen_table">
    <li class="calen_block">
        <h3>Sunday</h3>
        <div class="calen">
            <div class="calen_figure">
                <span class="calen_number">Style</span>
            </div>
        </div>
        <ul class="stuff">
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
        </ul>
        <div class="calfooter">
            <a href="#" class="action_button">Promo</a>
        </div>
    </li>
    <li class="calen_block">
        <h3>Monday</h3>
        <div class="calen">
            <div class="calen_figure">
                <span class="calen_number">Style</span>
            </div>
        </div>
        <ul class="stuff">
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
            <li>This!</li>
        </ul>
        <div class="calfooter">
            <a href="#" class="action_button">Promo</a>
        </div>
    </li>
and so on

我想做的是创建一个脚本,以便每个列的顺序由一周中的哪一天确定 - 因此,如果用户在周日访问该网站,则列表为周日、周一、周二...;如果用户在星期三访问该站点,则列表将在星期三、星期四、星期五...

我总是希望所有 7 个都显示,但希望在一周中的哪一天设置顺序。

我知道 var day_of_week = date.getDay();会给我一周中的哪一天,0-6 分别是周日到周六。我只是无法弄清楚如何最好地将其应用于这种情况(如果我应该有 7 组 7 组,并且有一个触发器来显示当天的正确组;或者如果

  • 实际上可以洗牌)。

    真正感激的任何想法。

  • 【问题讨论】:

    • 欢迎来到 SO。恐怕“任何想法”问题通常被认为是题外话。请努力并返回更具体的问题。
    • 你可以看看tinysort
    • @isherwood - 道歉;我知道我应该在来堡垒帮助之前走得更远。
    • @chenasraf - 这看起来是完美的解决方案!非常感谢您。

    标签: javascript jquery twitter-bootstrap date


    【解决方案1】:

    我会采用这样的方法:

    var blocks = $('.calen_block');
    var today = new Date();
    var day = today.getDay();
    var temp = blocks.splice(0,day); //remove all the days before current day
    temp = temp instanceof Array ? temp : [temp]; //splice doesn't return an array if you splice 1 element
    blocks = blocks.concat(temp); // add the elements on after the current day
    //empty the wrapper div and add them in the correct order
    

    这种方法是从一个 html 页面开始的,其中将日期从星期一添加到星期日。

    【讨论】:

    • 非常感谢!我可以将它与上面链接的“tinysort”结合使用来实现我的目标。感谢您的意见!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2020-01-04
    相关资源
    最近更新 更多