Html
    Css
    Js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div class="warp">
<!---->
<div class="search_box">
<input type="text" placeholder="">
<i class="search_icon"></i>
</div>
<!---->
<div class="search_content search_default">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
body {
background-color:#f5f5f5;
}
.warp {
width:50%;
margin:0 auto;
}
/*input*/
input {
border:0;
background-color:transparent;
}
/**/
.search_box {
width:60%;
height:32px;
box-sizing:border-box;
box-shadow:1px 1px 4px #e2e2e2;
padding:0 10px;
border:1px solid #e2e2e2;
border-radius:4px;
background-color:#f5f5f5;
margin:50px auto 20px auto;
}
.search_box input {
width:80%;
height:30px;
line-height:30px;
font-size:15px;
color:#8c8282;
}
.search_box .search_icon {
float:right;
display:inline-block;
width:24px;
height:24px;
background:url("img/search.png") no-repeat center center;
background-size:cover;
margin-top:3px;
}
/**/
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(function() {
var search_input = $(".search_box input"),
search_content = $(".search_content");
$(search_input).on("keyup", function() {
if (search_input.val() == '') {
$(search_content).show();
}
$(".search_content li:contains(" + search_input.val().trim() + ")").show();
$(".search_content li:not(:contains(" + search_input.val().trim() + "))").hide();
//
//$(".search_content li").hide().filter(":contains("+ search_input.val().trim() +")").show();
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
↑上面代码改变,会自动显示代码结果 jQuery调用版本:2.1.4
 立即下载

jQuery实现模糊搜索效果

思想: 当触发 keyup 键盘松开事件时,判断 input 输入框中的内容是不是 li 标签里包含的内容,包含则该  li 标签显示,不包含 则该 li 标签隐藏。

步骤:

1.使用 keyup 键盘松开事件,当键盘松开时触发以下内容。

2. 先获取 input 输入框中的内容。

3. 利用 jQuery 中 :contains 选择器判断 li 标签中的内容是否包含 input 输入框中的内容。

4.如果包含则该 li 标签显示,否则 则隐藏。

0