本篇文章介绍的内容是关于用JS做时钟的代码,现在分享给大家,有需要的朋友可以参考一下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" href="系统CSS初始化.css"> <style> ul{ height: 100px; width: 600px; margin: 200px auto; } img{ float:left; } span{ font-size: 100px; float: left; display: block; position: relative; bottom: 20px; } </style> <script> window.onload=function () { function setDoub(n) { if(n<10) { n='0'+n; } else { n=''+n; } return n; } var aImg=document.getElementsByTagName("img"); function tick() { var date=new Date(); var str=setDoub(date.getHours())+setDoub(date.getMinutes())+setDoub(date.getSeconds()); for(var i=0;i<aImg.length;i++) { aImg[i].src="images/"+str[i]+".png"; } } tick(); setInterval(tick,1000) } </script> </head> <body> <ul id="ul1"> <img src="images/0.png"> <img src="images/0.png"> <span>:</span> <img src="images/0.png"> <img src="images/0.png"> <span>:</span> <img src="images/0.png"> <img src="images/0.png"> </ul> </body> </html>
Atas ialah kandungan terperinci 用JS做时钟. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!