js寫的簡單購物車

來源:互聯網
上載者:User

標籤:

  最近在學習js的知識,對於我這種菜鳥而言,JS讓我太痛苦了。曾經也不知道聽誰說js很簡單的,所以就學了,學了才知道。。。都是坑。。。。

  現在分享一個我忍著巨大的痛苦寫的一個簡單購物車,並不是很完善。雖然不是我一個完成的,但是我是努力弄懂弄回才準備分享出去。有問題的部分請諒解,希望可以幫到有需要的朋友。

以下是購物車的簡單代碼:

(1)html

<body>  <div class="shopCar">    <div class="caozuo">      <label for="all">全選</label>      <input type="checkbox" id="all"/>          <label for="all">排序</label>      <input type="text" value=""/>    </div>    <ul>      <li>        <input type="checkbox"/>        <h3><a href="#">高跟鞋</a></h3>        <div>          <span class="add">+</span>          <input num="" type="text" value="1"/>          <span class="minus">-</span>        </div>        <span>¥:<i>1000</i></span>        <div>          <a href="javascript:void(0)">刪除</a>        </div>      </li>      <li>        <input type="checkbox"/>        <h3><a href="#">裙子</a></h3>        <div>          <span class="add">+</span>          <input num="" type="text" value="1"/>          <span class="minus">-</span>        </div>        <span>¥:<i>2900</i></span>        <div>          <a href="javascript:void(0)">刪除</a>        </div>      </li>      <li>        <input type="checkbox"/>        <h3><a href="#">可樂</a></h3>        <div>          <span class="add">+</span>          <input num="" type="text" value="1"/>          <span class="minus">-</span>        </div>        <span>¥:<i>1500</i></span>        <div>          <a href="javascript:void(0)">刪除</a>        </div>      </li>      <li>        <input type="checkbox"/>        <h3><a href="#">堅果</a></h3>        <div>          <span class="add">+</span>          <input num="" type="text" value="1"/>          <span class="minus">-</span>        </div>        <span>¥:<i>2000</i></span>        <div>          <a href="javascript:void(0)">刪除</a>        </div>      </li>    </ul>    <div>      <label for="allPrice">¥:</label>      <input id="allPrice" type="text" value="0">     </div>  </div></body>

(2)css樣式

*{      margin: 0;      padding: 0;      font-size: 48px;    }    a{      text-decoration: none;    }    ul{      list-style: none;    }    /* shopCar */    .shopCar{      margin-left: 20px;      margin-right: 20px;    }    .caozuo{      height: 100px;      border-bottom: 2px #999 solid;    }    ul{    }    li{      height: 100px;      background: #f5f5f5;      line-height: 100px;      border-bottom: 1px red solid;    }    li > *{      display: inline-block;    }    h3{      width: 330px;    }    [num=""]{      width: 100px;    }    li span{      width: 350px;    }

(3)js

    <script>        function $(x){          return document.querySelector(x);        }        function $s(x){          return document.querySelectorAll(x);        }        var Price=0;        var btns=$s("li [type=checkbox]");        var btn=$("#all");        var display=$("#allPrice");        var adds=$s(‘.add‘);        var minus=$s(‘.minus‘);        var shanchu=$s("li div a");        var length=btns.length;        // 計算總價        function allPrice(){            Price=0;            for(var i=0;i<length;i++){                if(btns[i].checked){                    Price += parseInt($s("[num=‘‘]")[i].value) * parseInt($s("li i")[i].innerHTML);                }            }            display.value=Price;        }        // 全選按鈕        btn.onclick=function(){            for(var i=0;i<length;i++){               btns[i].checked=this.checked;            }            if(this.checked){                allPrice();            }else{                Price = 0;                display.value = 0;            }        }        // 添加商品數量和減少商品數量        for(var i=0;i<adds.length;i++){            adds[i].index=i;            minus[i].index=i;            adds[i].onclick=function(){                var num=$s("[num=‘‘]")[this.index].value;                $s("[num=‘‘]")[this.index].value = ++num;                btns[this.index].checked=true;                allPrice();                panduan();            }            minus[i].onclick=function(){                var num=$s("[num=‘‘]")[this.index].value;                if(num <=1){                    $s("[num=‘‘]")[this.index].value = 1;                }                else{                    $s("[num=‘‘]")[this.index].value= --num;                    btns[this.index].checked=true;                }                allPrice();                panduan();            }        }        for(var i=0;i<length;i++){            btns[i].onclick=function(){                panduan();            }        }        // 根據商品複選框的條件判斷來判斷全選框是否全選        function panduan(){            for(var i=0;i<length;i++){                var isTrue=true;                if(btns[i].checked==false){                    btn.checked=false;                }                else if(btns[i].checked==true){                    for(var i=0;i<length;i++){                        if(btns[i].checked==false){                            isTrue=false;                         }                    }                    if(!isTrue){                        btn.checked=false;                    }                    else{                        btn.checked=true;                    }                }                allPrice();            }        }        // 刪除商品        for(var i=0;i<shanchu.length;i++){            shanchu[i].index=i;            shanchu[i].onclick=function(){                $s("li")[this.index].style.display = ‘none‘;                $s("li [type=‘checkbox‘]")[this.index].checked = false;                allPrice();            }        }    </script>

 

js寫的簡單購物車

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.