我想说“早上好,用户”(早上可以改为下午或晚上),我的代码无效。我很感激你的帮助。
index.html代码段
<h1 class="mx-3 text-center display-3">Good <span id="time"></span>, Username</h1>
master.js
var today = new Date()
var curHr = today.getHours()
var time = null;
if (curHr < 12) {
var time = "Morning";
} else if (curHr < 18) {
var time = "Afternoon";
} else {
var time = "Evening";
}
document.getElementById("time").innerHTML = time;
抱歉,我是javascript的菜鸟。
我也想知道这个错误意味着什么(来自谷歌Chrome控制台):
Uncaught TypeError: Cannot set property 'innerHTML' of null
at master.js:13
答案 0 :(得分:1)
如果您在跨度之前链接javascript文件,那么
Uncaught TypeError: Cannot set property 'innerHTML' of null at master.js:13
会发生,因为它还无法找到范围。尝试:
window.onload = function(e){
document.getElementById("time").innerHTML = time;
}