jQuery 数字时钟是一种使用 jQuery 库来实现动态显示当前时间的网页应用。它通过 JavaScript 和 jQuery 来更新页面上的时间显示,通常以24小时制或12小时制显示。
以下是一个简单的 jQuery 数字时钟示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Digital Clock</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#clock {
font-size: 48px;
text-align: center;
}
</style>
</head>
<body>
<div id="clock"></div>
<script>
function updateClock() {
const now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
hours = (hours < 10) ? '0' + hours : hours;
minutes = (minutes < 10) ? '0' + minutes : minutes;
seconds = (seconds < 10) ? '0' + seconds : seconds;
$('#clock').text(hours + ':' + minutes + ':' + seconds);
}
setInterval(updateClock, 1000);
updateClock(); // Initial call to display the time immediately
</script>
</body>
</html>
setInterval
的时间间隔设置不正确,或者 updateClock
函数没有正确调用。setInterval
的时间间隔设置为1000毫秒(1秒),并且 updateClock
函数在页面加载时被正确调用。通过以上示例代码和解决方法,你可以轻松创建一个简单的 jQuery 数字时钟,并解决常见的技术问题。
领取专属 10元无门槛券
手把手带您无忧上云