js实现在网页上简单显示时间的方法
本文实例讲述了js实现在网页上简单显示时间的方法。分享给大家供大家参考。具体如下:
这是一款网页时钟JS代码,纯javascript实现,显示时、分、秒。网页时间显示、网页时钟有很多,这个真的挺简易的,美工好的朋友可在此基础上进一步美化完善
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>简易网页时钟</title>
<style>
body,div{margin:0;padding:0;}
body{color:#fff;font:16px/1.5\5fae\8f6f\96c5\9ed1;}
#clock{width:300px;text-align:center;background:#1a1a1a;margin:10pxauto;padding:20px0;}
span{color:#000;width:80px;line-height:2;background:#fbfbfb;border:2pxsolid#b4b4b4;margin:010px;padding:010px;}
</style>
<script>
window.onload=function()
{
varoClock=document.getElementById("clock");
varaSpan=oClock.getElementsByTagName("span");
setInterval(getTimes,1000);
getTimes();
functiongetTimes()
{
varoDate=newDate();
varaDate=[oDate.getHours(),oDate.getMinutes(),oDate.getSeconds()];
for(variinaDate)aSpan[i].innerHTML=format(aDate[i])
}
functionformat(a)
{
returna.toString().replace(/^(\d)$/,"0$1")
}
}
</script>
</head>
<body>
<divid="clock">
<span></span>点<span></span>分<span></span>秒
</div>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。