doufen1933 2014-02-27 14:43
浏览 21
已采纳

根据日期更改div类

I'm building a little webpage to list the outstanding assignments I have at college.

Here's the code:

<div class="assignment">
    <div class="itemt green">DUE: 28/03/2014</div>
</div>

Here's the actual page: www.edavies.co/wkc

I would like the class green to be used if before the due date, the class amber for two weeks before the due date and finally the class red for one week before. If possible it would be cool to have black on the due date and afterwards.

Hope this makes sense. Anything is fine, PHP, JavaScript, jQuery.

  • 写回答

4条回答 默认 最新

  • dtuy84033 2014-02-27 16:24
    关注

    check http://jsfiddle.net/NhmW7/, test it changing those dates. the function i made for this problem.

    $( ".itemt" ).each(function() {
    
                var text=$(this).text();
                var res = text.split(" "); 
                var resta = res[1].split("/"); 
                var dd=resta[0];
                var mm=resta[1];
                var yy=resta[2];   // i couldnt test with dd/mm/yyy format, so i changed to mm/dd/yyy
    
                var now = new Date(); //"now"
                var duedate = new Date(mm+'/'+dd+'/'+yy)  // due date
                var diff = Math.abs(now-duedate); 
    
                 var each_day = 1000 * 60 * 60 * 24;//milliseconds in a day
                 var days = Math.round(diff / each_day);
    
                 if(days <= 14)
                     $(this).removeClass("green").addClass("amber");
                 if(days <= 7)
                     $(this).removeClass("amber").addClass("red");
                 if(days > 14)
                     $(this).addClass("green");
             });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计