您好,登錄后才能下訂單哦!
這篇文章主要介紹原生JS如何實現(xiàn)多條件篩選的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
我在學(xué)JS初始看到的教程基本都是JS二級聯(lián)動查詢、三級聯(lián)動查詢;如下圖:
但有時并不需要級聯(lián)查詢,如購買商品時:
今天我以慕課網(wǎng)前端樣式作為參考做了下面的Demo,基于原生JS
<div id="direction"> <strong>方向:</strong> <span class="active">全部</span> <span>前端</span> <span>后臺</span> <span>數(shù)據(jù)庫</span> <span>UI設(shè)計</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>基礎(chǔ)</span> <span>案例</span> <span>框架</span> <span>工具</span> </div> <strong>返回值:</strong> <p id="Res"></p>
<style> span{display: inline-block; cursor: pointer; padding: 8px; border: 1px solid #999;} span.active{ background-color: #c14d00; } </style>
<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>
最后附上效果圖:
注:本篇博文是 【無條件】的分類篩選
以上是原生JS如何實現(xiàn)多條件篩選的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。