<!DOCTYPE html>
<html>
<head>
  <title>动态电子表</title>
</head>
<body>
  <div id="clock"></div>
  <script>
    function updateClock() {
      var now = new Date();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();

      hours = (hours < 10 ? "0" : "") + hours;
      minutes = (minutes < 10 ? "0" : "") + minutes;
      seconds = (seconds < 10 ? "0" : "") + seconds;

      var timeString = hours + ":" + minutes + ":" + seconds;

      document.getElementById("clock").textContent = timeString;
    }

    setInterval(updateClock, 1000); // 每隔1秒更新一次时间
  </script>
</body>
</html>
在上面的代码中,我们定义了一个`updateClock`函数,该函数会获取当前时间,并将时间格式化为`hh:mm:ss`的形式。然后,我们使用`setInterval`函数每隔1秒调用一次`updateClock`函数,从而实现动态更新时间的效果。最后,我们通过`document.getElementById`方法获取到id为`clock`的元素,然后将格式化后的时间字符串赋值给该元素的`textContent`属性,从而在页面上显示出电子表。

原文地址: https://www.cveoy.top/t/topic/qcvg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录