網頁實時顯示當前時間 html+js
阿新 • • 發佈:2020-12-08
效果:
在網頁上能實時顯示當前的時間:
這個秒會一直走的。
實現程式碼
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標籤的結尾
總結
這樣便可顯示時間了,嘿嘿嘿~~