javascript实现搜索筛选功能

osc_np3y0rbq 2020-11-13 12:46:23
javascript


案例样式即功能
在这里插入图片描述

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<div class="search">
<input type="text" placeholder="请输入用户名">//搜索框
<p>//性别筛选按钮
<span sex='m'>male</span>
<span sex='f'>female</span>
<span sex='a' class="active">all</span>
</p>
</div>
<div class="user-list">//搜索列表,有js渲染添加
<ul>
</ul>
</div>
</div>
<script src="index.js"></script>
</body>
</html>

CSS代码:

*{

margin:0;
padding:0;
list-style: none;
}
.wrapper{

width:400px;
border:1px solid #ccc;
margin: 100px auto;
border-radius:4px;
}
.wrapper .search{

width:100%;
height:50px;
line-height:50px;
}
.wrapper .search input{

padding:10px 15px;
border-radius:4px;
margin-left:30px;
border:1px solid #ccc;
outline:none;
}
.wrapper .search p{

display:inline-block;
}
.wrapper .search p span{

color:#38f;
padding:3px 5px;
}
.wrapper .search p span.active{

color:#fff;
background-color:#38f;
border-radius: 4px;
}
.wrapper .user-list{

width:100%;
}
.wrapper .user-list ul{

width:100%;
}
.wrapper .user-list ul li{

border-bottom:1px solid #ccc;
position:relative;
padding:10px 0;
margin:0 30px;
}
.wrapper .user-list ul li img{

height:40px;
width:40px;
position:absolute;
}
.wrapper .user-list ul li p{

margin-left:50px;
width:300px;
color:rgb(117, 114, 114);
}

Javascript代码:

var oul=document.getElementsByTagName("ul")[0];
var oinput=document.getElementsByTagName("input")[0];
var text='',sex='a';//要将text和sex写在全局变量里面,这样可以记录上次操作的值为后续的页面渲染作准备;
var personArr=[
{
name:'王刚',src:'images/1.png',des:'颈椎不好',sex:'m'},
{
name:'刘颖',src:'images/2.png',des:'我是谁',sex:'f'},
{
name:'王秀英',src:'images/3.jpg',des:'我很好看',sex:'f'},
{
name:'刘金磊',src:'images/4.png',des:'你没有见过陌生人的脸',sex:'m'},
{
name:'刘飞仙',src:'images/5.jpg',des:'瓜皮刘',sex:'m'}
];
//页面渲染
function randerlist(arr){

var str='';
arr.forEach(function(elem,index){

str+='<li>\
<img src="'+elem.src+'">\
<p class="username">'+elem.name+'</p>\
<p class="des">'+elem.des+'</p>\
</li>'
});
oul.innerHTML=str;
}
randerlist(personArr);
//为性别筛选按钮设置点击事件,并获取每一次点击后的结果进行页面渲染。
var spans=document.getElementsByTagName("p")[0].children;
for(var i=0;i<spans.length;i++){

(function(j){

spans[j].onclick=function(){

//每点击一个按钮就要重新设置css属性
for(var k=0;k<spans.length;k++){

spans[k].className="";
}
this.className='active';
/*不要直接var lastArr=filterSex(this.innerHTML[0],personArr);,一定要写sex=this.innerHTML[0];
因为当点击了性别筛选按钮并输入姓名之后进行了一次渲染,再次删除姓名此时text=‘’,需要重新渲染,但并没有记
录上次点击性别筛选按钮后的sex值,此时他会默认为'a';*/
sex=this.innerHTML[0];
var lastArr=filterSex(sex,personArr);
lastArr=filterText(text,lastArr);
randerlist(lastArr);
}
})(i);
}
//获取搜索框内容,并重新渲染
oinput.oninput=function(){

text=this.value;
var newArr=filterText(text,personArr);
newArr=filterSex(sex,newArr);
randerlist(newArr);
}
//根据文本框的内容进行筛选,并返回筛选后的数组
function filterText(text,arr){

//filter函数,筛选作用,将return true的elem拼成一个数组并返回
return arr.filter(function(elem,index){

if(elem.name.indexOf(text)!=-1){

return true;
}else{

return false;
}
});
}
//根据性别进行筛选,并返回筛选后的数组
function filterSex(sex,arr){

if(sex=='a'){

return arr;
}else{

return arr.filter(function(elem,index){

if(elem.sex==sex){

return true;
}else{

return false;
}
})
}
}
版权声明
本文为[osc_np3y0rbq]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4365362/blog/4715703

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple