js實現(xiàn)模糊匹配功能
更新時間:2017年02月15日 16:38:46 作者:Sunshine_Lisa
這篇文章主要為大家詳細介紹了js實現(xiàn)模糊匹配功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
功能描述:
在搜索框中輸入某一個字段,可以查詢到相關的內(nèi)容
功能實現(xiàn):
1. 先聲明變量用于保存輸入框以及表格里面的內(nèi)容
2. 循環(huán)遍歷
遍歷表格每一行,查找匹配項
3. 判斷
如果輸入框里面的內(nèi)容跟表格中某個td的內(nèi)容相似,則表格中的某行就顯示,否則隱藏
功能實現(xiàn):
<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");
// 循環(huán)表格每一行,查找匹配項
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 href="#" class="header">A</a></li>
<li><a href="#">Angel</a></li>
<li><a href="#">Adobe</a></li>
<li><a href="#">Anne</a></li>
<li><a href="#" class="header">B</a></li>
<li><a href="#">Betty</a></li>
<li><a href="#">Bella</a></li>
<li><a href="#">Brown</a></li>
<li><a href="#" class="header">C</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Chris</a></li>
<li><a href="#">Claire</a></li>
<li><a href="#" class="header">D</a></li>
<li><a href="#">David</a></li>
<li><a href="#">Daniel</a></li>
<li><a href="#">Dora</a></li>
<li><a href="#" class="header">E</a></li>
<li><a href="#">Emily</a></li>
<li><a href="#">Elena</a></li>
<li><a 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');
// 循環(huán)所有列表,查找匹配項
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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript實現(xiàn)彈出模態(tài)窗體并接受傳值的方法
這篇文章主要介紹了JavaScript實現(xiàn)彈出模態(tài)窗體并接受傳值的方法,涉及JavaScript模態(tài)窗體的實現(xiàn)及基于URL的傳值操作技巧,需要的朋友可以參考下2016-02-02

