利用jquery实现一个时间数据来源于客户端的时钟
时间: 2023-10-27 22:04:51 浏览: 54
可以使用以下代码实现:
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>时钟</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<h1 id="clock"></h1>
<script>
$(document).ready(function(){
setInterval(function(){
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var time = hours + ':' + minutes + ':' + seconds;
$('#clock').html(time);
}, 1000);
});
</script>
</body>
</html>
```
代码解释:
1. 在头部引入 jQuery 库。
2. 在 `<body>` 中添加一个 `<h1>` 元素,用于显示时间。
3. 在 `<script>` 中使用 `setInterval()` 函数,每隔 1 秒刷新一次时间。
4. 获取当前时间,并拼接成字符串。
5. 用 jQuery 的 `html()` 方法将时间字符串显示在页面上。