相关文章推荐
var hours = now.getHours() ; var minutes = now.getMinutes() ; var seconds = now.getSeconds() ; var timeString = hours + ":" + minutes + ":" + seconds ; clock.innerHTML = timeString ; setInterval(updateClock, 1000) ;

在上面的代码中, document.getElementById("clock") 获取了HTML中的 <div> 元素, updateClock() 函数会每秒钟更新时钟的时间, setInterval() 函数用于每秒钟调用一次 updateClock() 函数。

最后,为了美化时钟的外观,你可以在CSS文件中设置 <div> 元素的样式。

代码示例: HTML:

<div id="clock"></div>
var clock = document.getElementById("clock");
function updateClock() {
  var now = new Date();
  var hours = now.getHours();
  var minutes = now.getMinutes();
  var seconds = now.getSeconds();
  var timeString = hours + ":" + minutes + ":" + seconds;
  clock.innerHTML = timeString;
setInterval(updateClock, 1000);
#clock {
  font-size: 2em;
  font-weight: bold;
  text-align: center;
  padding: 0.5em;
  background-color: #eee;
  border-radius: 5px;
  •  
    推荐文章