所以我试图创建一个每日消息。分区代码<div class="motd" id="shadowbox"> <h4 class="rainbow_text_animated" style="text-align: center;" >Message of the Day</h4> <p id="MOTDText" style="text-align: center;">nil</p> </div>脚本 function motdupdate() { var dailymessages = ["message1", "message2", "message3", "message4", "message5", "message6", "message7"] var today = new Date(); if (today.getDay() == 0 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[0]; else if (today.getDay() == 1 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[1]; else if (today.getDay() == 2 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[2]; else if (today.getDay() == 3 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[3]; else if (today.getDay() == 4 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[4]; else if (today.getDay() == 5 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[5]; else if (today.getDay() == 6 ) document.getElementById(motd.MOTDText).innerHTML = dailymessages[6]; }问题是,它一直给我这个错误:index.html:59 Uncaught ReferenceError: motd is not defined at motdupdate (index.html:59) at <anonymous>:1:1如何解决此问题?
2 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
getElementById(motd.MOTDText)
应该是
getElementById("MOTDText").innerHTML = dailymessages[6];
它们都应该更改为此才能正常工作。
12345678_0001
TA贡献1802条经验 获得超5个赞
首先,我认为今天.getDay()永远不会等于0。我不明白为什么你想这样称呼,因为在公共ID中必须是唯一的,所以你可能不依赖于div。试试像这样:
function motdupdate() {
var dailymessages = ["message1", "message2", "message3", "message4", "message5", "message6", "message7"]
var today = new Date();
document.getElementById("MOTDText").innerHTML = dailymessages[ parseInt(today.getDay() - 1)];
}
添加回答
举报
0/150
提交
取消