简体   繁体   中英

innerHTML Not Updating Text

I've been stumped on this bug where I am unable to update the innerHTML of my li classes named "currMonth" and "currYear". They are supposed to get updated by the OnStart() function. I tried using console.log and it showed that the values were changing behind the scenes. It would be great if someone can show me what I'm doing wrong. Thanks!

HTML:

<!DOCTYPE html>
<html lang = "en-US">

<head>
  <title title = "O(n)Time"></title>
  <meta charset="UTF-8">
  <script src="../js/script.js"></script>
  <link rel="stylesheet" type="text/css" href="../css/styles.css">
</head>

<body>
  <div class="month">
    <ul>
      <li class="prev">&#10094;</li>
      <li class="next">&#10095;</li>
      <li id="currMonth"></li>
      <li id="currYear"><span style="font-size:18px"></span></li>
    </ul>
  </div>
</body>

<ul class="weekdays">
  <li>Sunday</li>
  <li>Monday</li>
  <li>Tuesday</li>
  <li>Wednesday</li>
  <li>Thursday</li>
  <li>Friday</li>
  <li>Saturday</li>
</ul>

<ul class="days">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li><span class="active">10</span></li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
  <li>21</li>
  <li>22</li>
  <li>23</li>
  <li>24</li>
  <li>25</li>
  <li>26</li>
  <li>27</li>
  <li>29</li>
  <li>30</li>
  <li>31</li>
</ul>

</html>

CSS:

ul {
  list-style-type: none;
}

body{
  font-family: Verdana, sans-serif;
}

/* Month header */
.month{
  padding: 70px 25px;
  width: auto;
  background: #1abc9c;
  text-align: center;
}

/* Month list */
.month ul {
  margin: 0;
  padding: 0;
}

.month ul li{
  color: white;
  font-size: 20px;
  text-transform: uppercase;
  letter-spacing: 3px;
}

/* Previous button inside month header */
.month .prev {
  float: left;
  padding-top: 10px;
}

/* Next button */
.month .next {
  float: right;
  padding-top: 10px;
}

/* Weekdays (Sun-Sat) */
.weekdays {
  margin: 0;
  padding: 10px 0;
  background-color: #ddd;
}

.weekdays li {
  display: inline-block;
  width: 13.6%;
  color: #666;
  text-align: center;
}

/* Days (1-31) */
.days {
  padding: 10px 0;
  background: #eee;
  margin: 0;
}

.days li {
  list-style-type: none;
  display: inline-block;
  width: 13.6%;
  text-align: center;
  margin-bottom: 5px;
  font-size: 12px;
  color: #777;
}

/* Highlight of the "current" day */
.days li .active {
  padding: 5px;
  background: #1abc9c;
  color: white !important
}

JS:

//Constants
MONTHS = ["January", "February", "March", "April", "May", "June", "July",
          "August", "September", "October", "November", "December"];

//Variables
  var prevArrow = document.getElementsByClassName('prev');
  var nextArrow = document.getElementsByClassName('next');

OnStart(MONTHS);

//Functions
function OnStart(MONTHS){
  var d = new Date();
  var month = d.getMonth();
  var year = d.getFullYear();

  document.getElementById('currMonth').innerHTML = MONTHS[month];
  document.getElementById('currYear').innerHTML = year;

}

getElementsByClassName() returns collection. You have to use proper index with that.

Change:

document.getElementsByClassName('currMonth').innerHTML = MONTHS[month];
document.getElementsByClassName('currYear').innerHTML = year;

To:

document.getElementsByClassName('currMonth')[0].innerHTML = MONTHS[month];
document.getElementsByClassName('currYear')[0].innerHTML = year;

You get an array back even though there may be only one result hence you need to reference the 0'th element like so.

document.getElementsByClassName('currMonth')[0].innerHTML = MONTHS[month];


document.getElementsByClassName('currYear')[0].innerHTML = year;

replace .getElementsbyClassName to querySelector

? That works?

document.getElementsByClassName('currMonth') returns a nodelist. you'll need to iterate through each node and then assign the innerHTML. That's why you cannot assign it like the way you are doing. You can alternatively use document.querySelectorAll

let list = document.getElementsByClassName('currMonth');
// list = document.querySelectorAll('.currMonth'); either or
let foo = [].slice.call(list); // convert to array of values from node list

foo.forEach(function(objNode) {
    objNode.innerHTML = "value"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM