javascript--时钟

本文详细解析了一段HTML代码,该代码用于创建一个实时显示的时钟,通过JavaScript实现时间的动态更新,并提供了开始和停止时钟的功能。文章涵盖了HTML、CSS和JavaScript的基本使用,适合初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>时钟</title>
    <meta name="keywords" content="关键字列表" />
    <meta name="description" content="网页描述" />
    <link rel="stylesheet" type="text/css" href="" />
    <style type="text/css">
        #li{
            width:120px;
            height:20px;
            border:1px red solid;
        }
    </style>
    <script>
    //定义加载事件
        window.οnlοad=function(){
        //定义变量
        var time;
        var t;
        //实现开始显示事件功能的函数
        function display(){
        //实例化
        myDate=new Date();
        //获取时间戳
        time=myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
        //将获取的时间戳显示在要加载的内容上
        $('li').innerHTML=time;
        //利用定时器调用自己
        t=setTimeout(display,1000);
        }
        //实现停止时间的函数
        function display1(){
            //清楚定时器
            clearTimeout(t);
        }
            //获取DOM对象的函数
            function $(id){
            //返回对象
            return document.getElementById(id);
        }    //触发开始事件
            document.getElementById('star').οnclick=function(){
                
                display();
            }
            //触发结束事件
            document.getElementById('end').οnclick=function(){
                
                display1();
            }
            
        }

        
        
    </script>
</head>
<body>
    <div id='li'></div>
    <input id='star'type="button" name="button"value='开始'/>
    <input id='end'type='button' name='button'value="结束"/>
</body>
</html>

 

转载于:https://www.cnblogs.com/IT-Computer-TcpIp/p/4951711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值