Blogger Information
Blog 12
fans 0
comment 0
visits 6072
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js编程学习之实战购物车
天空
Original
522 people have browsed it

购物车实战

实现效果

作业效果图

实现购物车: 只计算选中商品,并同时更新相关数据的主要思路方法

  • 定义全局checks变量,获取checkbox的NodeList
  • 自动计算函数中,获取商品数量数组时,判断checks[i]的选中状态,如果未选中,数量数组对应的值为0,否则为正常值。
  • 在全选框按钮的onchange事件中调用自动计算函数
  • 在选中框的onchange事件中调用自动计算函数

    代码实现

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>购物车</title>
    8. <style>
    9. .box {
    10. width: 22em;
    11. height: 2em;
    12. }
    13. .list>li {
    14. height: 1.6em;
    15. background-color: #efefef;
    16. display: grid;
    17. grid-template-columns: repeat(5, 3em);
    18. gap: 1em;
    19. place-items: center right;
    20. border-bottom: 1px solid #ccc;
    21. }
    22. .list>li:first-of-type {
    23. background-color: lightseagreen;
    24. color: white;
    25. }
    26. .list>li:hover:not(:first-of-type) {
    27. cursor: pointer;
    28. background-color: lightcyan;
    29. }
    30. .list>li input[type='number'] {
    31. width: 3em;
    32. border: none;
    33. outline: none;
    34. text-align: center;
    35. font-size: 1em;
    36. background-color: transparent;
    37. }
    38. .list>li:last-of-type span.total-num,
    39. .list>li:last-of-type span.total-amount {
    40. grid-column: span 2;
    41. place-self: center right;
    42. color: lightseagreen;
    43. }
    44. .account {
    45. float: right;
    46. background-color: lightseagreen;
    47. color: white;
    48. border: none;
    49. outline: none;
    50. width: 4.5em;
    51. height: 1.8em;
    52. }
    53. .account:hover {
    54. background-color: coral;
    55. cursor: pointer;
    56. }
    57. </style>
    58. </head>
    59. <body>
    60. <div class="box">
    61. <div class="selectAll">
    62. <!-- checkAll(): 当全选按钮的状态发生变化化,触发该事件 change -->
    63. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
    64. <label for="check-all">全选</label>
    65. </div>
    66. <ul class="list">
    67. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
    68. <li>
    69. <input type="checkbox" onchange="checkItems()" checked />
    70. <span class="content">手机</span>
    71. <input type="number" value="1" min="1" class="num" />
    72. <span class="price">100</span>
    73. <span class="amount">0</span>
    74. </li>
    75. <li>
    76. <input type="checkbox" onchange="checkItems()" checked />
    77. <span class="content">电脑</span>
    78. <input type="number" value="2" min="1" class="num" />
    79. <span class="price">200</span><span class="amount">0</span>
    80. </li>
    81. <li>
    82. <input type="checkbox" onchange="checkItems()" checked />
    83. <span class="content">相机</span>
    84. <input type="number" value="3" min="1" class="num" />
    85. <span class="price">300</span>
    86. <span class="amount">0</span>
    87. </li>
    88. <li>
    89. <span>总计:</span>
    90. <span class="total-num">0</span>
    91. <span class="total-amount">0</span>
    92. </li>
    93. </ul>
    94. <button class="account">结算</button>
    95. </div>
    96. <script>
    97. // 1.全选函数
    98. function checkAll() {
    99. // 1.全选按钮状态
    100. let status = event.target.checked;
    101. // 2.根据状态动态设定所有商品的状态
    102. let items=document.querySelectorAll('.list li input[type="checkbox"]');
    103. items.forEach(item=>(item.checked=status));
    104. autoCalculate();
    105. }
    106. // 2.根据用户选择来动态设置全选状态
    107. function checkItems() {
    108. // 1.拿到全部商品
    109. let items=document.querySelectorAll('.list li input[type="checkbox"]');
    110. // 2.判断状态,只有全部被选择,才需要设置全选框为true
    111. let status=[...items].every(item=>item.checked===true);
    112. // 3.将这个新状态设置到全选按钮
    113. document.querySelector('.check-all').checked=status;
    114. autoCalculate();
    115. }
    116. // 3.商品自动计算
    117. // 所有计算都是基于商品数量变
    118. const nums=document.querySelectorAll('.num');
    119. const checks=document.querySelectorAll('.list li input[type="checkbox"]');
    120. console.log(checks);
    121. // 3.1 计算总数量
    122. function getTotalNum(numArr) {
    123. return numArr.reduce((acc,cur)=>acc+cur);
    124. }
    125. // 3.2计算每个商品的金额
    126. function getAmount(numArr, priceArr) {
    127. //金额=数量*单价
    128. return numArr.map((num,index)=>num*priceArr[index]);
    129. }
    130. // 3.3计算总金额
    131. function getTotalAmount(amountArr) {
    132. return amountArr.reduce((acc,cur)=>acc+cur);
    133. }
    134. // 3.4自动计算
    135. function autoCalculate() {
    136. // 商品数量数组
    137. // const numArr=[...nums].map(num=>parseInt(num.value));
    138. // 如果没选中数量为0
    139. const numArr=[...nums].map((num,index)=>checks[index].checked?parseInt(num.value):0);
    140. // 单价数组
    141. const prices=document.querySelectorAll('.price');
    142. const priceArr = [...prices].map(price=>parseFloat(price.textContent));
    143. // console.log(priceArr);
    144. // 金额数组
    145. const amountArr=getAmount(numArr,priceArr);
    146. // console.log(amountArr);
    147. //总数量
    148. document.querySelector('.total-num').textContent=getTotalNum(numArr);
    149. // 总金额
    150. document.querySelector('.total-amount').textContent=getTotalAmount(amountArr);
    151. // 为每个商品填充金额
    152. document.querySelectorAll('.amount').forEach((amount,index)=>amount.textContent=amountArr[index]);
    153. }
    154. // 当页面加载时自动计算
    155. window.onload=autoCalculate;
    156. // 当数量更新时,自动计算所有数据
    157. nums.forEach(num=>(num.onchange=autoCalculate));
    158. </script>
    159. </body>
    160. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post