innerHTML不更新文本

时间:2018-04-20 23:30:07

标签: javascript html css

我一直难以理解这个错误,我无法更新名为“currMonth”和“currYear”的li类的innerHTML。它们应该由OnStart()函数更新。我尝试使用console.log,它表明价值在幕后发生了变化。如果有人能告诉我我做错了什么会很棒。谢谢!

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;

}

4 个答案:

答案 0 :(得分:4)

getElementsByClassName()返回集合。你必须使用适当的索引。

变化:

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

要:

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

答案 1 :(得分:0)

即使可能只有一个结果,你得到一个数组,因此你需要像这样引用第0个元素。

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


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

答案 2 :(得分:0)

将.getElementsbyClassName替换为querySelector

? 那有用吗?

答案 3 :(得分:0)

document.getElementsByClassName('currMonth')返回节点列表。你需要迭代每个节点,然后分配innerHTML。这就是为什么你不能像你做的那样分配它。您也可以使用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"
}