2012-07-31 254 views
-1

我有这样的天列表修改元素的背景颜色,根据日期

<ul> 
    <li class="dark"><a rel="25" class="0725" style="color: rgb(255, 255, 255);">25</a></li> 
    <li class="dark"><a rel="26" class="0726" style="color: rgb(255, 255, 255);">26</a></li> 
    <li class="dark"><a rel="27" class="0727" style="color: rgb(255, 255, 255);">27</a></li> 
    <li class="dark"><a rel="28" class="0728" style="color: rgb(255, 255, 255);">28</a></li> 
    <li class="dark"><a rel="29" class="0729" style="color: rgb(255, 255, 255);">29</a></li> 
    <li class="dark"><a rel="30" class="0730" style="color: rgb(255, 255, 255);">30</a></li> 
    <li class="dark first"><a rel="31" class="0731" style="color: rgb(255, 255, 255);">31</a></li> 
    <li class="dark"><a rel="1" class="0801" style="color: rgb(255, 255, 255);">1</a></li> 
    <li class="dark"><a rel="2" class="0802" style="color: rgb(255, 255, 255);">2</a></li> 
    <li class="dark"><a rel="3" class="0803" style="color: rgb(255, 255, 255);">3</a></li> 
    <li class="dark"><a rel="4" class="0804" style="color: rgb(255, 255, 255);">4</a></li> 
    <li class="dark"><a rel="5" class="0805" style="color: rgb(255, 255, 255);">5</a></li> 
    <li class="dark"><a rel="6" class="0806" style="color: rgb(255, 255, 255);">6</a></li> 
    <li class="dark"><a rel="7" class="0807" style="color: rgb(255, 255, 255);">7</a></li> 
    <li class="dark"><a rel="8" class="0808" style="color: rgb(255, 255, 255);">8</a></li> 
    <li class="dark"><a rel="9" class="0809" style="color: rgb(255, 255, 255);">9</a></li> 
    <li class="dark"><a rel="10" class="0810" style="color: rgb(255, 255, 255);">10</a></li> 
    <li class="dark"><a rel="11" class="0811" style="color: rgb(255, 255, 255);">11</a></li> 
    <li class="dark"><a rel="12" class="0812" style="color: rgb(255, 255, 255);">12</a></li> 
</ul> 

,我想改变当前天的颜色和之前的日子另一种颜色。 该课程是月份格式。

+5

这与密码保护有什么关系? – Ohgodwhy 2012-07-31 14:49:40

+0

请学习如何接受答案...如果您不知道我在说什么[点击此处](http://meta.stackexchange.com/a/5235/170679) – ManseUK 2012-07-31 14:54:37

+0

对不起,密码的问题是一个错误,正确的问题是: 我想根据今天的日期和前几天来着色一天。 – roybatty 2012-07-31 14:56:42

回答

0

尝试这样的事情

// get current date 
var today = new Date(); 
// get days using getDate() and pad to 2 digits 
var tdd = zeroPad(today.getDate(),2); 
// get months using getMonth() and pad to 2 digits 
var tmm = zeroPad(today.getMonth()+1,2); 

// get current date 
var yesterday = new Date(); 
// set date to date -1 ie yesterday 
yesterday.setDate(today.getDate() - 1); 
// get days using getDate() and pad to 2 digits 
var ydd = zeroPad(yesterday.getDate(),2); 
// get months using getMonth() and pad to 2 digits 
var ymm = zeroPad(yesterday.getMonth()+1,2); 

// concatenate strings to form class and use css() to set appropriate color 
$('.'+tmm + tdd).css('background-color','red'); 
$('.'+ymm + ydd).css('background-color','blue'); 

应设置为红色,颜色为今天和颜色昨日蓝色。使用来自SO answer here

Working example here

0

用户的JS采取的Date object.css()zeroPad()功能:

var d = new Date(); 
alert(d.getDate()); 
alert(d.getMonth()); 

d.getDate()将显示1和31 g.getMonth(之间的数)返回月份(0-11)

取而代之的是使用0731作为你的类,把0731作为ID, d使用ByID getter更新样式类。

http://www.w3schools.com/jsref/jsref_obj_date.asp

可以帮助自己现在这些信息。