Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
filtrar lista HTML con JS

No estoy muy familiarizado con HTML y JavaScript, por lo que mi aprendizaje se basa en código de ingeniería inversa.

El código que estoy usando filtra una tabla basada en una palabra clave modificada. También puedo agregar este código a un botón y filtrar una palabra clave. He agregado un conmutador muy primitivo que funciona (bueno, alterna de todos modos). Sin embargo, no puedo hacer que la tabla cambie todos los estilos de nuevo a style="Display = ""; al menos no puedo hacer que vuelvan a aparecer?

 var myTog = 0; function myFunction() { var ul, li, a, i, txtValue; if (myTog == 0) { myTog = 1; ul = document.getElementById("myUL"); li = ul.getElementsByTagName("li"); for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; txtValue = a.textContent || a.innerText; if (txtValue.indexOf("Cindy") > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } else { alert("test if toggel works"); // ================ // Trouble Section: // ================ for (i = 0; i < li.length; i++) { li[i].style.display = ""; } // =============== myTog = 0; } }
 <ul id="myUL"> <li><a href="#">Adele</a></li> <li><a href="#">Agnes</a></li> <li><a href="#">Billy</a></li> <li><a href="#">Bob</a></li> <li><a href="#">Calvin</a></li> <li><a href="#">Christina</a></li> <li><a href="#">Cindy</a></li> </ul> <button type="button" onclick="myFunction()">Click Me!</button>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

si está tratando de mostrar el li, entonces puede usar algo como:

 li[i].style.display = "block";
about 4 years ago · Juan Pablo Isaza Report

0

Debe definir su li = ul.getElementsByTagName("li"); fuera de la cláusula if

 var myTog = 0; function myFunction() { var ul, li, a, i, txtValue; ul = document.getElementById("myUL"); li = ul.getElementsByTagName("li"); if (myTog == 0) { myTog = 1; for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; txtValue = a.textContent || a.innerText; if (txtValue.indexOf("Cindy") > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } else { for (i = 0; i < li.length; i++) { li[i].style.display = ""; } myTog = 0; } }
 <ul id="myUL"> <li><a href="#">Adele</a></li> <li><a href="#">Agnes</a></li> <li><a href="#">Billy</a></li> <li><a href="#">Bob</a></li> <li><a href="#">Calvin</a></li> <li><a href="#">Christina</a></li> <li><a href="#">Cindy</a></li> </ul> <button type="button" onclick="myFunction()">Click Me!</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!