<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> span{ font-size: 5em; color: red; } </style> </head> <body> <h2>计算日期间隔</h2> <p><label>请选择日期:</label><input id="d2" type="date"/></p> <p><input type="button" value="计算" id="btn"/><span id="show"></span></p> <script type="text/javascript"> var d2=document.getElementById("d2"); var btn=document.getElementById("btn"); var show=document.getElementById("show"); btn.onclick=function(){ var begin,end,diff; if(d2.value==""){ show.innerText="请日期选择!"; }else{ begin=now.getTime(); end=getTimeStamp(d2.vale); diff=Math.abs(begin-end)/1000/60/60/24 ; show.innerText=d1.value +"和" +d2.value+"相差"+diff+"天"; } } function getTimeStamp(dateString){ var date=new Date(dateString); return date.getTime(); } </script> </body> </html>改正
时间: 2023-08-14 13:22:53 浏览: 87
这段代码有几个错误和不严谨之处,需要进行如下改正:
1. 在第13行代码中,应该将 `d1.value` 改为 `d2.value`,即:
```
show.innerText = "今天和" + d2.value + "相差" + diff + "天";
```
2. 在第14行代码中,应该将 `getTimeStamp(d2.vale)` 改为 `getTimeStamp(d2.value)`,即:
```
end = getTimeStamp(d2.value);
```
3. 在第16行代码中,应该将 `d1.value` 改为 `d2.value`,即:
```
show.innerText = "今天和" + d2.value + "相差" + diff + "天";
```
4. 在第11行代码中,应该给 `now` 变量赋值,即:
```
var now = new Date();
```
5. 在第10行代码中,应该将 `p` 标签改为 `div` 标签,以符合 HTML5 规范,即:
```
<div>
<label>请选择日期:</label>
<input id="d2" type="date" />
</div>
```
改正后的代码如下:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算日期间隔</title>
<style type="text/css">
span {
font-size: 5em;
color: red;
}
</style>
</head>
<body>
<h2>计算日期间隔</h2>
<div>
<label>请选择日期:</label>
<input id="d2" type="date" />
</div>
<div>
<input type="button" value="计算" id="btn" />
<span id="show"></span>
</div>
<script type="text/javascript">
var d2 = document.getElementById("d2");
var btn = document.getElementById("btn");
var show = document.getElementById("show");
btn.onclick = function() {
var now = new Date();
var begin, end, diff;
if (d2.value == "") {
show.innerText = "请选择日期!";
} else {
begin = now.getTime();
end = getTimeStamp(d2.value);
diff = Math.abs(begin - end) / 1000 / 60 / 60 / 24;
show.innerText = "今天和" + d2.value + "相差" + diff + "天";
}
}
function getTimeStamp(dateString) {
var date = new Date(dateString);
return date.getTime();
}
</script>
</body>
</html>
```
阅读全文