简体   繁体   English

如何 - 在纯 javascript 中过滤/搜索表

[英]How TO - Filter/Search Table in pure javascript

Search table using pure javascript.使用纯 javascript 搜索表。 I had tried below function.我在function下面试过了。 What's wrong in this function?这个 function 有什么问题?

 function myFunction() { var input, filter, table, tr, td, i, txtValue; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; }else{ tr[i].style.display = "none"; } } } }

I don't see a problem your code seems to work我没有看到您的代码似乎工作的问题

 function myFunction() { var input, filter, table, tr, td, i, txtValue; input = document.getElementById("myInput"); //filter = input.value.toUpperCase(); filter = '1' table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } setTimeout(myFunction, 1000)
 <table id="myTable"> <tr> <td>1</td> <td>2</td> </tr> <tr> <td>11</td> <td>33</td> </tr> <tr> <td>2</td> <td>2</td> </tr> <tr> <td>3</td> <td>4</td> </tr> <tr> <td>3</td> <td>5</td> </tr> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM