黑底白字电子时钟
时间: 2023-11-21 09:06:19 浏览: 51
好的,下面是实现黑底白字电子时钟的 HTML 和 CSS 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>电子时钟</title>
<style>
body {
background-color: #000;
color: #fff;
font-size: 3em;
font-family: monospace;
text-align: center;
padding-top: 50px;
}
</style>
</head>
<body>
<div id="clock"></div>
<script>
function updateTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var timeString = hours + ':' + minutes + ':' + seconds;
document.getElementById('clock').innerHTML = timeString;
}
setInterval(updateTime, 1000);
</script>
</body>
</html>
```
这段代码中,我们使用了 CSS 来设置页面的背景颜色、字体颜色、字体大小、字体类型和文本对齐方式。
在 HTML 中,我们使用了一个 `div` 元素来显示数字时钟。在 JavaScript 中,我们使用了与前面相同的 `updateTime` 函数,将时、分、秒转换成字符串,然后将其拼接在一起,最后更新 `div` 元素的内容。
这样,我们就实现了一个黑底白字的电子时钟。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)