搜索

首页  >  问答  >  正文

当我运行它时,按钮没有执行应有的操作

我不知道为什么按钮不起作用。我想要做的是,当我单击按钮时,每个选中“卡”框的神奇宝贝不再出现。它不起作用。我刚刚开始编码,当你读到这篇文章时,你可能会微笑。这一切都非常令人困惑。

function showUnowned() {
  var rows = document.getElementsByTagName("tr");
  for (var i = 0; i < rows.length; i++) {
    if (rows[i].getElementsByTagName("input")[2].checked == true) {
      rows[i].style.display = "none";
    }
  }
}
<h1>My Pokemon Card Collection</h1>
<table>
  <tr>
    <th>Picture</th>
    <th>Name</th>
    <th>Pokedex Number</th>
    <th>Card</th>
    <th>Other</th>
  </tr>
  <!-- This is an example of how to add a Pokemon to the table. You can copy and paste this code for each Pokemon in your collection. -->
  <tr>
    <td><img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"></td>
    <td>Bulbasaur</td>
    <td>1</td>
    <td><input type="checkbox"></td>
    <td><input type="checkbox"></td>
  </tr>
  <tr>
    <td><img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/2.png"></td>
    <td>Ivysaur</td>
    <td>2</td>
    <td><input type="checkbox"></td>
    <td><input type="checkbox"></td>
  </tr>
  <tr>
    <td><img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/3.png"></td>
    <td>Venusaur</td>
    <td>3</td>
    <td><input type="checkbox"></td>
    <td><input type="checkbox"></td>
  </tr>
  <tr>
    <td><img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png"></td>
    <td>Charmander</td>
    <td>4</td>
    <td><input type="checkbox"></td>
    <td><input type="checkbox"></td>
  </tr>



</table>
<button onclick="showUnowned()">Show Unowned Pokemon</button>

P粉068486220P粉068486220482 天前526

全部回复(1)我来回复

  • P粉418854048

    P粉4188540482023-09-14 09:37:21

    您再次循环遍历这些元素(有 4 个元素,但循环了 5 次),因此您可以编辑代码来解决此问题:

    function showUnowned() {
      var rows = document.getElementsByTagName("tr");
      for (var i = 1; i < rows.length; i++) {
        if (rows[i].getElementsByTagName("input")[2].checked == true) {
          rows[i].style.display = "none";
        }
      }
    }
    

    回复
    0
  • 取消回复