小编Jul*_*ang的帖子

循环遍历HTML表格中的元素

我正在学习Javascript,在学校作业中我们必须使用循环计数在2014年制作了多少游戏.

它在控制台中没有返回任何内容,我哪里出错了?

var allGames = document.getElementsByTagName('td');
var array = Array.prototype.slice.call(allGames, 0)
var games14 = 0;
for (var i = 0; i < array.length; ++i) {
  if (array[i] == 2014) {
    games14++;
    console.log(games14)
  }
}
Run Code Online (Sandbox Code Playgroud)
<table id="games">
  <thead>
    <tr>
      <th>Titel</th>
      <th>Genre</th>
      <th>Årstal</th>
    </tr>
  </thead>
  <tbody id="games_tbody">
    <tr class="horror">
      <td class="title">Outlast</td>
      <td>Horror</td>
      <td>2013</td>
    </tr>
    <tr class="rpg">
      <td class="title">Dragon Age: Inquisition</td>
      <td>Role-playing Game</td>
      <td>2014</td>
    </tr>
    <tr class="rpg">
      <td class="title">Skyrim</td>
      <td>Role-playing Game</td>
      <td>2011</td>
    </tr>
    <tr class="horror">
      <td class="title">Amnesia: The Dark Descent</td>
      <td>Horror</td>
      <td>2010</td>
    </tr> …
Run Code Online (Sandbox Code Playgroud)

html javascript arrays dom loops

1
推荐指数
1
解决办法
3963
查看次数

标签 统计

arrays ×1

dom ×1

html ×1

javascript ×1

loops ×1