|
效果如图
代码如下(带注释):
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>不懂网-小鑫啊</title> </head> <body> 现在是: <span id="year"></span>年 <span id="month"></span>月 <span id="day"></span>日 星期 <span id="week"></span> <span id="h"></span>时 <span id="m"></span>分 <span id="s"></span>秒 <script type="text/javascript">var year = document.getElementById('year'), month = document.getElementById('month'), day = document.getElementById('day'), week = document.getElementById('week'), h = document.getElementById('h'), m = document.getElementById('m'), s = document.getElementById('s');function timed() {var time = new Date();year.innerHTML=time.getFullYear();month.innerHTML=time.getMonth()+1;day.innerHTML=time.getDate();week.innerHTML = time.getDay();h.innerHTML=time.getHours();m.innerHTML=(time.getMinutes() > 9 ? time.getMinutes() : "0" + time.getMinutes());s.innerHTML=(time.getSeconds() > 9 ? time.getSeconds() : "0" + time.getSeconds());w = time.getDay(); //定义个数组并赋值 var weekArr = ['天','一','二','三','四','五','六'];for(var i = 0;i<weekArr.length;i++) { //如果w的值等于i的值 if (w == i) { //就更改week的内容为weekArr //也就是中文的数字 week.innerHTML= weekArr; } } //setTimeout函数设置间隔 每1000毫秒执行一次函数 setTimeout("timed()",1000);}timed();</script> </body></html>
|
|