gpt4 book ai didi

javascript时钟项目不显示到页面

转载 作者:行者123 更新时间:2023-11-29 16:34:33 26 4
gpt4 key购买 nike

我查看了一些 javascript 时钟项目并自行制作了一些项目,但我无法显示时钟。不确定我错过了什么,但我无法在页面上显示实际时钟。这就是我所需要的,但堆栈溢出让我写得更多,因为显然我不能写一篇主要是代码的帖子。

<script>
function updateClock() {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();

// Add a 0 if # is under 10 instead of leaving a blank space
currentMinutes = ( currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "") + currentSeconds;

// Conversion to 12-hour time instead of 24-hour time
var timeOfDay = ( currentHours < 12) ? "AM" : "PM";
currentHours = ( currentHours > 12) ? currentHours - 12 : currentHours;
currentHours = ( currentHours == 0) ? 12 : currentHours;

document.getElementById('.clock')[0].innerHTML =
currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;



}
</script>
body{
background: rgb(238, 216, 216);
}

#header {
text-align: center;
color: black;
text-shadow: 2px 2px 2px rgb(136, 181, 230);
font-size: 35px;
}

.container {
text-align: center;
}

#clock {
font-size: 4em;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />

</head>
<body>
<div id="header">
<h1>Clock Project</h1>
</div>

<span class="clock"></span>

</body>

最佳答案

您不能使用 document.getElementById 有一个类名。

要么设置id你的跨度到“时钟”(<span id="clock"></span>)并得到这样的元素:document.getElementById("clock")

或使用 document.getElementsByClassName 得到这样的元素:document.getElementsByClassName("clock")[0]

由于您的 CSS 使用 id 为元素设置样式时钟与 #clock cssRule,您的意图可能是设置 idspan到“时钟”。

你应该使用 setInterval 每 1000 毫秒更新一次时钟(您实际上并没有调用 updateClock 函数)。

body{
background: rgb(238, 216, 216);
}

#header {
text-align: center;
color: black;
text-shadow: 2px 2px 2px rgb(136, 181, 230);
font-size: 35px;
}

.container {
text-align: center;
}

#clock {
font-size: 4em;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />

</head>
<body>
<div id="header">
<h1>Clock Project</h1>
</div>

<span id="clock"></span>
<script>
function updateClock() {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();

// Add a 0 if # is under 10 instead of leaving a blank space
currentMinutes = ( currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "") + currentSeconds;

// Conversion to 12-hour time instead of 24-hour time
var timeOfDay = ( currentHours < 12) ? "AM" : "PM";
currentHours = ( currentHours > 12) ? currentHours - 12 : currentHours;
currentHours = ( currentHours == 0) ? 12 : currentHours;

document.getElementById('clock').innerHTML =
currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
}
setInterval(function(){updateClock()}, 1000);
</script>
</body>

您可能想使用 Date.prototype.toLocaleTimeString() 这样您就不必进行那么多的计算来获取日期。

在这种情况下,new Date().toLocaleTimeString("en-US")应该做你想做的事。

body{
background: rgb(238, 216, 216);
}

#header {
text-align: center;
color: black;
text-shadow: 2px 2px 2px rgb(136, 181, 230);
font-size: 35px;
}

.container {
text-align: center;
}

#clock {
font-size: 4em;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />

</head>
<body>
<div id="header">
<h1>Clock Project</h1>
</div>

<span id="clock"></span>
<script>
var clock = document.getElementById("clock");
function updateClock() {
clock.innerHTML = new Date().toLocaleTimeString("en-US");
}
setInterval(function(){
updateClock()
}, 1000);
</script>

关于javascript时钟项目不显示到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52788247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com