js實現簡單商品篩選功能

本文實例為大傢分享瞭js實現商品篩選功能的具體代碼,供大傢參考,具體內容如下

應用場景:商品篩選

<!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>手機:</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>價格:</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 = []; //存放三個類別的option

 for (var i = 0; i < li.length; i++) {
  // 獲取每個li中的option進行處理
  var options = li[i].querySelectorAll("a");
  for (var j = 0; j < options.length; j++) {
  // 當選項被點擊時,傳入點擊的類別是第幾個li的
  // options[j].onclick = Aclick.bind(options[j], [i]); //1. 將點擊事件分離出去
  options[j].onclick = Bclick(i); //2. 將點擊事件分離出去
  }
 }
</script>

當寫到這裡的時候想要把這個事件函數分離出去,但是有參數。於是問題出現

如何將一個帶參數的事件函數分離出去???

於是乎:就有瞭以下兩種方法

1、使用bind改變this指向後返回一個不執行的函數

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中的空項,重新向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、在註冊函數外面套一層函數將註冊函數返回

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中的空項,重新向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]);
  }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: