</style> </head> <script> var hour_box = document.querySelector('.hour') var min_box = document.querySelector('.min') var sec_box = document.querySelector('.sec') function getTime(){ var now_time =new Data() var end_time = new Date('2023-5-16 12:00:00') var time = end_time- now_time var hour =Math.floor(time/3600) var min =Math.floor((time-hour*60)/60) var sec =Math.floor(time-hour*3600-min*60) hour = hour < 12?'0'+hour:hour min = min < 12?'0'+min:min sec = sec < 12?'0'+sec:sec hour_box.innerText = hour min_box.innerText = min sec_box.innerText = sec } getTime() setInterval(getTime,1000) </script> <body onload="countTime()"> <div class="box"> <div class="title">京东秒杀 </div><div class="desc">12:00场 距结束 </div><div class="items"> <div class="hour">h</div> <div class="time">:</div> <div class="min">m</div> <div class="time">:</div> <div class="sec">s</div> </div> </div> </body>哪里有错
时间: 2023-06-19 15:09:04 浏览: 129
有几个地方有错误,看起来是拼写错误:
1. 第4行应该是 `var now_time = new Date();` 而不是 `var now_time =new Data()`
2. 第5行应该是 `var end_time = new Date('2023-05-16 12:00:00');` 而不是 `var end_time = new Date('2023-5-16 12:00:00')`
3. 第8行应该是 `var min =Math.floor((time-hour*3600)/60);` 而不是 `var min =Math.floor((time-hour*60)/60);`
4. 第9行应该是 `var sec =Math.floor(time-hour*3600-min*60);` 而不是 `var sec =Math.floor(time-hour*3600-min*60);`
5. 第11行应该是 `hour = hour < 10 ? '0'+hour : hour;` 而不是 `hour = hour < 12?'0'+hour:hour`
6. 第12行应该是 `min = min < 10 ? '0'+min : min;` 而不是 `min = min < 12?'0'+min:min`
7. 第13行应该是 `sec = sec < 10 ? '0'+sec : sec;` 而不是 `sec = sec < 12?'0'+sec:sec`
修改后的代码如下:
```
<head>
<style>
.box {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f2f2f2;
}
.title {
font-size: 36px;
font-weight: bold;
margin-bottom: 20px;
}
.desc {
font-size: 24px;
margin-bottom: 30px;
}
.items {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
font-size: 48px;
font-weight: bold;
}
.time {
margin: 0 10px;
}
</style>
</head>
<body onload="countTime()">
<div class="box">
<div class="title">京东秒杀</div>
<div class="desc">12:00场 距结束</div>
<div class="items">
<div class="hour">h</div>
<div class="time">:</div>
<div class="min">m</div>
<div class="time">:</div>
<div class="sec">s</div>
</div>
</div>
<script>
var hour_box = document.querySelector('.hour');
var min_box = document.querySelector('.min');
var sec_box = document.querySelector('.sec');
function countTime() {
var now_time = new Date();
var end_time = new Date('2023-05-16 12:00:00');
var time = end_time - now_time;
var hour = Math.floor(time / 3600000);
var min = Math.floor((time - hour * 3600000) / 60000);
var sec = Math.floor((time - hour * 3600000 - min * 60000) / 1000);
hour = hour < 10 ? '0' + hour : hour;
min = min < 10 ? '0' + min : min;
sec = sec < 10 ? '0' + sec : sec;
hour_box.innerText = hour;
min_box.innerText = min;
sec_box.innerText = sec;
}
setInterval(countTime, 1000);
</script>
</body>
```
阅读全文