我想按以下格式顯示日期和日期,但它不適用於我。顯示「Uncaught TypeError:無法設置屬性'innerHTML'爲null」錯誤。任何人都可以幫我解決這個問題嗎?我的下面的JavaScript代碼有什麼問題?
輸出: -
今天是:週三
當前時間:晚上8點:25:20
**我的HTML代碼: - **
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Excercise</title>
<script src="Js\DateTimeExcercise.js">
</script>
</head>
<body>
Today is : <span id="day"></span>
<br/><br/>
Current time is : <span id="time"></span>
</body>
</html>
**我的Java腳本代碼: - **
var day, time, Sec, Min, Hr, AMPM;
AMPM = "AM";
day = new Date().getDay();
Sec = new Date().getSeconds();
Min = new Date().getMinutes();
Hr = new Date().getHours();
if (Hr>12)
{
Hr = Hr - 12;
AMPM = "PM";
}
switch(day)
{
case 0:
txt = "Sunday";
break;
case 1:
txt = "Monday";
break;
case 2:
txt = "Tueday";
break;
case 3:
txt = "Wednesday";
break;
case 4:
txt = "Thrusday";
break;
case 5:
txt = "Friday";
break;
case 6:
txt = "Saturday";
break;
}
document.getElementById("day").innerHTML = txt;
document.getElementById("time").innerHTML = Hr + " "+ AMPM + " "+ ":" + Min + ":" + Sec;
DOMContentLoaded。 – dfsq
你剛剛閉幕