JavaScript 实时更新 DIV 内容显示当前时间
<div id='time' style='width: 150px; height: 150px;'></div>
<script>
function updateTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeString = hours + ':' + minutes + ':' + seconds;
document.getElementById('time').innerHTML = timeString;
}
setInterval(updateTime, 1000);
</script>
<p>这段代码将在页面上创建一个 150px * 150px 的 DIV,并使用 JavaScript 每秒更新一次 DIV 中的文本内容,以显示当前时间。</p>
原文地址: https://www.cveoy.top/t/topic/nWhI 著作权归作者所有。请勿转载和采集!