当我尝试将这些结果显示到我的页面中的函数时,它会返回一个错误。错误:无法设置属性“innerHTML”的值:对象为空或未定义
脚本有效,它们不是我的。我从网上找到的工作示例中得到它们。
EDITEDL:我确实改变了它,但仍然得到同样的错误
<!DOCTYPE html>
<html>
<head>
<style>
#header {
font-size: 5em;
}
</style>
<script type="text/javascript">
function updateclock() {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = (currentHours == 0) ? 12 : currentHours;
// Update the time display
document.getElementById("clock").innerHTML = (currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay);
}
function updatedate() {
var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date();
var curr_day = d.getDay();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date == 31) {
sup = "st";
}
else if (curr_date == 2 || curr_date == 22) {
sup = "nd";
}
else if (curr_date == 3 || curr_date == 23) {
sup = "rd";
}
else {
sup = "th";
}
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.getElementById("date").innerHTML = (d_names[curr_day] + " " + curr_date + "<SUP>" + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
}
updateclock();
setInterval(updateclock, 1000);
updatedate();
setInterval(updatedate, 1000);
</script>
</head>
<body>
<table>
<tr>
<td>
</td>
<td style="text-align=center" id =header>
</td>
<td>
<span id="date"> </span>
<span id="clock"> </span>
</td>
</tr>
</table>
</body>
</html>