gpt4 book ai didi

javascript - 无法设置属性值 'innerHTML' : object is null or undefined

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:53 24 4
gpt4 key购买 nike

当我尝试将这些结果显示到我页面中的函数时,它返回错误。错误:无法设置属性“innerHTML”的值:对象为空或未定义

脚本有效,它们不是我的。我从网上找到的工作示例中得到它们。

编辑:我确实更改了它,但仍然出现相同的错误

<!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">&nbsp;</span>
<span id="clock">&nbsp;</span>
</td>
</tr>
</table>
</body>
</html>

最佳答案

问题是您的初始 updateclock()updatedate()在 DOM 准备好之前调用函数。可以这样想:

  1. 您的浏览器加载页面。
  2. 它命中了 <script>标记并开始运行 JavaScript。
  3. 它会尝试运行您的函数并从 DOM 中获取一个项目(即 document.getElementById("date") )。该元素尚未加载,因此它返回 null。如果对象不存在,则不能调用 .innerHTML在上面。

要解决此问题,您必须在加载时运行脚本。在 jQuery 中,这是:

$(function() {
updateclock();
setInterval(updateclock, 1000);

updatedate();
setInterval(updatedate, 1000);
}

否则,您可以将它们放在一个函数中,然后添加 onload="functionName()"属性到 body 标签。

此外,正如其他人提到的,您应该删除 # s 在 ID 中。

关于javascript - 无法设置属性值 'innerHTML' : object is null or undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710629/

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