首页 > 代码库 > Javascript 电子时钟源码

Javascript 电子时钟源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!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 = "http://www.mamicode.com/images/" + str[i] + ".jpg";
                aImg[i].src = http://www.mamicode.com/"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=http://www.mamicode.com/"images/0.jpg" />
    <img src=http://www.mamicode.com/"images/0.jpg" />
    :
    <img src=http://www.mamicode.com/"images/0.jpg" />
    <img src=http://www.mamicode.com/"images/0.jpg" />
    :
    <img src=http://www.mamicode.com/"images/0.jpg" />
    <img src=http://www.mamicode.com/"images/0.jpg" />
</body>
</html>