网页实时显示当前时间 html+js

效果:

在网页上能实时显示当前的时间:
1
这个秒会一直走的。

实现代码

1.在body标签里添加如下代码:

<body onload="time()">
</body>

2.定义一个盒子标签来显示时间,也可以用其它标签,id设为“showtime”,如:

<h2 id="showtime"></h2>

3.js代码实现功能:

<script>
/* 时间 */
function time() {
     
     
  t_div = document.getElementById('showtime');
  var now = new Date()
  t_div.innerHTML = now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate() + "/" + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + "";
  setTimeout(time, 1000);
}
</script>

js代码可以让在body标签的结尾

总结

这样便可显示时间了,嘿嘿嘿~~

猜你喜欢

转载自blog.csdn.net/luo1831251387/article/details/110846526