天气晴朗,天空万里无云,温暖的阳光洒在人的身上,就像是一位母亲轻轻地抚摸着我们,微风徐徐,波光粼粼,站在美丽的湖边尽情享受大自然的无穷魅力。
功能描述:
在搜索框中输入某一个字段,可以查询到相关的内容
功能实现:
1. 先声明变量用于保存输入框以及表格里面的内容
2. 循环遍历
遍历表格每一行,查找匹配项
3. 判断
如果输入框里面的内容跟表格中某个td的内容相似,则表格中的某行就显示,否则隐藏
功能实现:
<html> <head> <style> #myInput { width: 100%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } #myTable { border-collapse: collapse; width: 100%; border: 1px solid #ddd; font-size: 18px; } #myTable th, #myTable td { text-align: left; padding: 12px; } #myTable tr { border-bottom: 1px solid #ddd; } #myTable tr.header, #myTable tr:hover { background-color: #f1f1f1; } </style> </head> <body> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="搜索..."> <table id="myTable"> <tr class="header"> <th style="width:60%;">项目名称</th> <th style="width:40%;">时间</th> </tr> <tr> <td>redPackets</td> <td>2017.2.6</td> </tr> <tr> <td>traffic</td> <td>2016.12.25</td> </tr> <tr> <td>creditCard</td> <td>2017.1.18</td> </tr> <tr> <td>returnMoney</td> <td>2016.11.25</td> </tr> </table> <script> function myFunction() { // 声明变量 var input, filter, table, tr, td, i; 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) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } </script> </body> </html>
扩展:模拟通讯录搜索提示
<html> <head> <style> #myInput { width: 100%; font-size: 16px; /* 加大字体 */ padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } #myUL { list-style-type: none; padding: 0; margin: 0; } #myUL li a { border: 1px solid #ddd; /* 链接添加边框 */ margin-top: -1px; background-color: #f6f6f6; padding: 12px; text-decoration: none; font-size: 18px; color: black; display: block; } #myUL li a.header { background-color: #5587A2; cursor: default; } #myUL li a:hover:not(.header) { background-color: #eee; } </style> </head> <body> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="搜索..."> <ul id="myUL"> <li><a rel="nofollow noopener noreferrer" href="#" class="header">A</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Angel</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Adobe</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Anne</a></li> <li><a rel="nofollow noopener noreferrer" href="#" class="header">B</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Betty</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Bella</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Brown</a></li> <li><a rel="nofollow noopener noreferrer" href="#" class="header">C</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Calvin</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Chris</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Claire</a></li> <li><a rel="nofollow noopener noreferrer" href="#" class="header">D</a></li> <li><a rel="nofollow noopener noreferrer" href="#">David</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Daniel</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Dora</a></li> <li><a rel="nofollow noopener noreferrer" href="#" class="header">E</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Emily</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Elena</a></li> <li><a rel="nofollow noopener noreferrer" href="#">Eufemia</a></li> </ul> <script> function myFunction() { // 声明变量 var input, filter, ul, li, a, i; input = document.getElementById('myInput'); filter = input.value.toUpperCase(); ul = document.getElementById("myUL"); li = ul.getElementsByTagName('li'); // 循环所有列表,查找匹配项 for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } </script> </body> </html>
到此这篇关于js实现模糊匹配功能就介绍到这了。志在山顶的人,不会贪念山腰的风景。更多相关js实现模糊匹配功能内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!