日期:2014-05-16  浏览次数:20332 次

如何跟天数加颜色
小弟现在在做一个倒计时的小功能!!时间格式是三位数  var days = Math.floor(hours/24);
比如 210天  010天   009 天 !现在是要对每个位数加颜色!days 就是这个三位数输出来的值!要的效果是这样子

比如当 210天时
    当 010天时
    当  009天时

现在的问题是 要怎么把dsys出来的值加颜色!还怎么设置他的格式是三位数 比如当是9 怎么变成 009

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var a=[
"<span style='color:red'>0</span>",
"<span style='color:blue'>1</span>",
"<span style='color:green'>2</span>",
"<span style='color:pink'>3</span>",
"<span style='color:yellow'>4</span>",
"<span style='color:red'>5</span>",
"<span style='color:rgb(100,100,100)'>6</span>",
"<span style='color:rgb(150,200,100)'>7</span>",
"<span style='color:rgb(250,200,50)'>8</span>",
"<span style='color:rgb(150,0,100)'>9</span>",
]
window.onload=function(){
var d=parseInt(Math.random()*1000)+"";
var str="";
for(var i=0;i<d.length;i++){
str+=a[d.charAt(i)]
}
document.getElementById("test").innerHTML=str;
}
</script>
</head>

<body>
<div id="test"></div>
</body>
</html>
类似这样试试
------解决方案--------------------

dsys=9;
document.write( ('000'+days).slice(-3).replace(/(\d)$/,'<font color="blue">$1</font>').replace(/(\d)\b/,'<font color="green">$1</font>')  )