其他分享
首页 > 其他分享> > 原生JS实现多条件筛选

原生JS实现多条件筛选

作者:互联网

https://www.jb51.net/article/193662.htm

原生JS实现多条件筛选

本文实例为大家分享了原生JS实现多条件筛选的具体代码,供大家参考,具体内容如下

我在学JS初始看到的教程基本都是JS二级联动查询、三级联动查询;如下图:

但有时并不需要级联查询,如购买商品时:

今天我以慕课网前端样式作为参考做了下面的Demo,基于原生JS

?
12345678910111213141516171819202122232425262728<div id="direction"> <strong>方向:</strong> <span class="active">全部</span> <span>前端</span> <span>后台</span> <span>数据库</span> <span>UI设计</span></div><div id="category"> <strong>分类:</strong> <span class="active">全部</span> <span>HTML/CSS</span> <span>JavaScript</span> <span>jQuery</span> <span>Python</span> <span>Java</span> <span>AngularJS</span></div><div id="type"> <strong>类型:</strong> <span class="active">全部</span> <span>基础</span> <span>案例</span> <span>框架</span> <span>工具</span></div><strong>返回值:</strong><p id="Res"></p>
?
1234567<style> span{display: inline-block;  cursor: pointer; padding: 8px; border: 1px solid #999;} span.active{  background-color: #c14d00; }</style>
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445<script> var dSpan = document.getElementById('direction').getElementsByTagName('span'); var cSpan = document.getElementById('category').getElementsByTagName('span'); var tSpan = document.getElementById('type').getElementsByTagName('span'); var aSpan = document.getElementsByTagName('span'); var oDirection = document.getElementById('direction'); var oCategory = document.getElementById('category'); var oType = document.getElementById('type'); var oRes = document.getElementById('Res'); dSpan[0].className = 'active'; cSpan[0].className = 'active'; tSpan[0].className = 'active'; for(var i=0; i<aSpan.length; i++){  aSpan[i].onclick = function(){   var siblings = this.parentNode.children;   for(var j=0; j<siblings.length; j++){    siblings[j].className = '';   }   this.className = 'active';   if(this.parentNode == oDirection || this.parentNode == oCategory || this.parentNode == oType){    returnRes();   }  } }  function returnRes(){  var o1 = 0, o2 = 0, o3 = 0;  for(var i=0; i<dSpan.length; i++){   if(dSpan[i].className == 'active'){    o1 = i;   }  }  for(var i=0; i<cSpan.length; i++){   if(cSpan[i].className == 'active'){    o2 = i;   }  }  for(var i=0; i<tSpan.length; i++){   if(tSpan[i].className == 'active'){    o3 = i;   }  }  oRes.innerHTML = (dSpan[o1].innerHTML + "," + cSpan[o2].innerHTML + "," + tSpan[o3].innerHTML); }</script>

最后附上效果图:

注:本篇博文是 【无条件】的分类筛选

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

                        <div class="art_xg">
                            <b>您可能感兴趣的文章:</b><ul><li><a href="/article/124840.htm" title="基于JavaScript实现前端数据多条件筛选功能" target="_blank">基于JavaScript实现前端数据多条件筛选功能</a></li><li><a href="/article/107385.htm" title="Vue.js实现多条件筛选、搜索、排序及分页的表格功能" target="_blank">Vue.js实现多条件筛选、搜索、排序及分页的表格功能</a></li><li><a href="/article/103420.htm" title="js实现表格筛选功能" target="_blank">js实现表格筛选功能</a></li><li><a href="/article/116413.htm" title="JS实现商品筛选功能" target="_blank">JS实现商品筛选功能</a></li><li><a href="/article/89574.htm" title="AngularJs页面筛选标签小功能" target="_blank">AngularJs页面筛选标签小功能</a></li><li><a href="/article/173005.htm" title="原生js实现商品筛选功能" target="_blank">原生js实现商品筛选功能</a></li></ul>
                        </div>

                    </div>

标签:原生,document,span,JS,className,active,var,筛选,strong
来源: https://www.cnblogs.com/sunny3158/p/15105236.html