jquery实现搜索框功能实例详解
搜索框实现搜索一个ul列表中的指定关键词的li。
html代码:
<ul class="todo-content"> <li class="todo-ltem"> <div class="todo-tip"> <p>fhasjfas</p> </div> <div class="todo-btnlist"> <button class="btn-finish">完成</button> <button class="btn-finish">修改</button> <button class="btn-finish">删除</button> </div> </li> <li class="todo-ltem"> <div class="todo-tip"> <p>fhasjfas</p> </div> <div class="todo-btnlist"> <button class="btn-finish">完成</button> <button class="btn-finish">修改</button> <button class="btn-finish">删除</button> </div> <li class="todo-ltem"> <div class="todo-tip"> <p>你好</p> </div> <div class="todo-btnlist"> <button class="btn-finish">完成</button> <button class="btn-finish">修改</button> <button class="btn-finish">删除</button> </div> <li class="todo-ltem"> <div class="todo-tip"> <p>你好啊</p> </div> <div class="todo-btnlist"> <button class="btn-finish">完成</button> <button class="btn-finish">修改</button> <button class="btn-finish">删除</button> </div> </li> </ul>
js:
当回车(keycode为13)按下时,获取输入框(id为findinput)的值,并保存在keyword关键字中,利用filter()对li进行筛选。
将原有内容隐藏,筛选出的内容进行渐变显示。
$('#findinput').keydown(function(event){ if(event.keyCode == 13){ var keyword = $(this).val() // alert(keyword) $('li').hide()//将原有的内容隐藏 //然后将含有keyword的li进行渐变显示 $("p").filter(":Contains("+keyword+")").parents('.todo-ltem').fadeIn(2000) } })
效果如下:
未筛选时:
筛选后:
总结
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17