编程实现在页面中显示电子时钟,代码
时间: 2024-05-19 12:15:37 浏览: 138
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>电子时钟</title>
<style>
body {
font-size: 5rem;
text-align: center;
background: #1D1F20;
color: #FFF;
}
</style>
<script>
setInterval(function() {
// 获取当前时间
var now = new Date();
// 获取小时、分钟、秒钟
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// 为单个数字添加前导零
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
// 更新页面上的时钟
document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds;
}, 1000); // 每秒更新一次
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
阅读全文