您好,登錄后才能下訂單哦!
小編給大家分享一下js如何實(shí)現(xiàn)簡單商品篩選功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
js實(shí)現(xiàn)商品篩選功能的具體代碼如下
應(yīng)用場(chǎng)景:商品篩選
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> * { margin: 0; padding: 0; list-style: none; text-decoration: none; } .choose { width: 500px; height: auto; margin: auto; } .choose nav { height: 50px; background-color: red; } .choose nav span { margin: 0 5px; } .choose .show { color: red; } .choose ul li { border: 1px solid black; } .choose ul li a { box-sizing: border-box; display: inline-block; width: 40px; border-left: 1px solid black; margin: 5px; padding-left: 5px; } </style> </head> <body> <div class="choose"> <nav></nav> <ul> <li> <strong>手機(jī):</strong> <a href="javascript:;">小米</a> <a href="javascript:;">華為</a> <a href="javascript:;">apple</a> <a href="javascript:;">OPPO</a> <a href="javascript:;">vivo</a> </li> <li> <strong>價(jià)格:</strong> <a href="javascript:;">3200</a> <a href="javascript:;">2600</a> <a href="javascript:;">899</a> <a href="javascript:;">2799</a> <a href="javascript:;">2299</a> </li> <li> <strong>屏幕:</strong> <a href="javascript:;">399</a> <a href="javascript:;">600</a> <a href="javascript:;">800</a> <a href="javascript:;">1200</a> </li> </ul> </div> </body> </html>
<script type="text/javascript"> var li = document.querySelectorAll('li'); var stack = []; //存放三個(gè)類別的option for (var i = 0; i < li.length; i++) { // 獲取每個(gè)li中的option進(jìn)行處理 var options = li[i].querySelectorAll("a"); for (var j = 0; j < options.length; j++) { // 當(dāng)選項(xiàng)被點(diǎn)擊時(shí),傳入點(diǎn)擊的類別是第幾個(gè)li的 // options[j].onclick = Aclick.bind(options[j], [i]); //1. 將點(diǎn)擊事件分離出去 options[j].onclick = Bclick(i); //2. 將點(diǎn)擊事件分離出去 } } </script>
當(dāng)寫到這里的時(shí)候想要把這個(gè)事件函數(shù)分離出去,但是有參數(shù)。于是問題出現(xiàn)
如何將一個(gè)帶參數(shù)的事件函數(shù)分離出去???
于是乎:就有了以下兩種方法
1、使用bind改變this指向后返回一個(gè)不執(zhí)行的函數(shù)
function Aclick(index) { // 刪掉nav var choose = document.querySelector('.choose'); var nav = document.querySelector(".choose nav"); choose.removeChild(nav); // 使用stack重新添加 stack[index] = this.innerHTML; var nav = document.createElement("nav"); for (k = 0; k < stack.length; k++) { if (stack[k] != "" && stack[k] != undefined) { //略過stack中的空項(xiàng),重新向nav中添加span var span = document.createElement("span"); var a = document.createElement("a"); a.innerHTML = "X"; a.href = "javascript:void(0);"; span.innerHTML = stack[k]; a.onclick = function() { stack[index] = ""; nav.removeChild(this.parentNode); } span.appendChild(a); nav.appendChild(span); } } choose.insertBefore(nav, choose.children[0]); }
2、在注冊(cè)函數(shù)外面套一層函數(shù)將注冊(cè)函數(shù)返回
function Bclick(index) { return function() { // 刪掉nav var choose = document.querySelector('.choose'); var nav = document.querySelector(".choose nav"); choose.removeChild(nav); // 使用stack重新添加 stack[index] = this.innerHTML; var nav = document.createElement("nav"); for (k = 0; k < stack.length; k++) { if (stack[k] != "" && stack[k] != undefined) { //略過stack中的空項(xiàng),重新向nav中添加span var span = document.createElement("span"); var a = document.createElement("a"); a.innerHTML = "X"; a.href = "javascript:void(0);"; span.innerHTML = stack[k]; a.onclick = function() { stack[index] = ""; nav.removeChild(this.parentNode); } span.appendChild(a); nav.appendChild(span); } } choose.insertBefore(nav, choose.children[0]); } }
看完了這篇文章,相信你對(duì)“js如何實(shí)現(xiàn)簡單商品篩選功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。