Maison  >  Questions et réponses  >  le corps du texte

Quand je l'exécute, le bouton ne fait pas ce qu'il devrait

Je ne sais pas pourquoi le bouton ne fonctionne pas. Ce que je veux faire, c'est que tous les Pokémon dont la case "bloqué" est cochée n'apparaissent plus lorsque je clique sur le bouton. Ça ne marche pas. Je viens de commencer à coder et en lisant ceci, vous souriez probablement. Tout cela est très confus.

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粉068486220371 Il y a quelques jours444

répondre à tous(1)je répondrai

  • P粉418854048

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

    Vous parcourez à nouveau les éléments (il y a 4 éléments, mais vous bouclez 5 fois), vous pouvez donc modifier votre code pour résoudre ce problème :

    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";
        }
      }
    }
    

    répondre
    0
  • Annulerrépondre