猿问

如何在 div 中调用<p> id?

所以我试图创建一个每日消息。分区代码


<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

如何解决此问题?


交互式爱情
浏览 84回答 2
2回答

守候你守候我

getElementById(motd.MOTDText)应该是getElementById("MOTDText").innerHTML&nbsp;=&nbsp;dailymessages[6];它们都应该更改为此才能正常工作。

12345678_0001

首先,我认为今天.getDay()永远不会等于0。我不明白为什么你想这样称呼,因为在公共ID中必须是唯一的,所以你可能不依赖于div。试试像这样:function motdupdate() {&nbsp; &nbsp; var dailymessages = ["message1", "message2", "message3", "message4", "message5", "message6", "message7"]&nbsp; &nbsp; var today = new Date();&nbsp; &nbsp; &nbsp;document.getElementById("MOTDText").innerHTML = dailymessages[ parseInt(today.getDay() - 1)];&nbsp;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答