<!DOCTYPE html> <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"> window.onload = function () { //有一秒钟的延迟,所以onload的时候需要先执行一次 move(); setInterval(move, 1000); } function move() { var oDate = new Date(); var str = toTwo(oDate.getHours()) + toTwo(oDate.getMinutes()) + toTwo(oDate.getSeconds()); var aImg = document.getElementsByTagName("img"); for (var i = 0; i < aImg.length; i++) { //str[i]这种写法只有高版本浏览器兼容,所以最好换成charAt // aImg[i].src = "images/" + str[i] + ".jpg"; aImg[i].src = "images/" + str.charAt(i) + ".jpg"; } } //个位数的补零 function toTwo(n) { if (n < 10) { return '0' + n; } else { return n.toString(); } } //日期方法拓展 function getTime(){ var oDate = new Date(); oDate.getDate();//日期 oDate.getFullYear();//年 oDate.getMonth() + 1;//月 oDate.getDay();//星期,从零开始的,0是星期日,1是星期一 } </script> </head> <body> <img src="images/0.jpg" /> <img src="images/0.jpg" /> : <img src="images/0.jpg" /> <img src="images/0.jpg" /> : <img src="images/0.jpg" /> <img src="images/0.jpg" /> </body> </html>