Blogger Information
Blog 35
fans 0
comment 0
visits 16736
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车全选功能
手机用户311660634
Original
430 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. .cart {
  10. width: 460px;
  11. display: grid;
  12. gap: 10px;
  13. }
  14. table {
  15. border-collapse: collapse;
  16. text-align: center;
  17. }
  18. table caption {
  19. font-size: 20px;
  20. margin-bottom: 10px;
  21. }
  22. table input[type='number'] {
  23. width: 40px;
  24. }
  25. table th,
  26. table td {
  27. border-bottom: thin solid #888;
  28. padding: 5px;
  29. }
  30. table thead {
  31. border-top: thin solid #888;
  32. background-color: lightcyan;
  33. }
  34. table tfoot {
  35. background-color: lightcyan;
  36. }
  37. table tbody tr:nth-child(odd):hover {
  38. background-color: #eee;
  39. cursor: pointer;
  40. }
  41. table tr td:first-child {
  42. padding-left: 20px;
  43. }
  44. table tr td:last-child {
  45. padding-right: 20px;
  46. }
  47. .cart .pay {
  48. display: grid;
  49. grid: 1fr / auto-flow;
  50. place-content: end;
  51. gap: 10px;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div class="cart">
  57. <table>
  58. <caption>
  59. 我的购物车
  60. </caption>
  61. <thead>
  62. <tr>
  63. <!-- 全选按钮 -->
  64. <td><input type="checkbox" name="" class="checkall" checked /></td>
  65. <td>编号</td>
  66. <td>品名</td>
  67. <td>单位</td>
  68. <td>单价</td>
  69. <td>数量</td>
  70. <td>金额(元)</td>
  71. </tr>
  72. </thead>
  73. </table>
  74. </div>
  75. <script type="module">
  76. // (一) 商品信息
  77. const items = [
  78. { id: 286, name: '酸奶', units: '箱', price: 50, num: 1 },
  79. { id: 870, name: '苹果', units: '千克', price: 10, num: 1 },
  80. { id: 633, name: '外套', units: '件', price: 300, num: 1 },
  81. { id: 153, name: '皮鞋', units: '双', price: 400, num: 1 },
  82. { id: 109, name: '手机', units: '台', price: 5000, num: 1 },
  83. ];
  84. // (二) 导入购物车模块
  85. import Cart from './modules/cart.js';
  86. // (三) 实例化购物车类
  87. const cart = new Cart(items);
  88. // console.log(cart.total);
  89. // console.log(cart.money);
  90. // console.log(cart.totalMoney);
  91. // (四) 获取购物车(表格)
  92. const table = document.querySelector('table');
  93. // (五) 将商品渲染到购物车元素中 tbody
  94. // 1 创建 tbody: 商品容器
  95. const tbody = table.createTBody();
  96. // 2. 创建tbody的内容,商品列表
  97. items.forEach(function (item, key) {
  98. // 1. 创建商品模板字符串
  99. const tr = `
  100. <tr>
  101. <td><input type="checkbox" name="" class="check" checked /></td>
  102. <td>${item.id}</td>
  103. <td>${item.name}</td>
  104. <td>${item.units}</td>
  105. <td>${item.price}</td>
  106. <td>
  107. <input type="number" name="" value="${item.num}"
  108. </td>
  109. <td class="money">${cart.money[key]}</td>
  110. </tr>
  111. `;
  112. // 2. 将内容填充到tbody
  113. tbody.insertAdjacentHTML('beforeend', tr);
  114. // tbody.innerHTML = tr;
  115. });
  116. // (六) 将相关统计数据(总数量,总金额),填充到tfoot中
  117. const tfoot = table.createTFoot();
  118. // 创建tfoot内容
  119. let tr = `
  120. <tr>
  121. <td colspan="5">总计:</td>
  122. <td class="total">${cart.total}</td>
  123. <td class="total-money">${cart.totalMoney}</td>
  124. </tr>
  125. `;
  126. tfoot.insertAdjacentHTML('beforeend', tr);
  127. // (七) 更新数量,实时计算出结果并显示出来
  128. // 1. 拿到所有的数量控件
  129. const nums = table.querySelectorAll('input[type=number]');
  130. // 2. 为每一个数量控件添加事件监听: input
  131. nums.forEach(function (num, key) {
  132. num.oninput = function () {
  133. // 1. 计算总数量
  134. items[key].num = num.value * 1;
  135. cart.total = cart.getTotal(items);
  136. // 2. 计算每个商品金额
  137. cart.money[key] = num.value * 1 * items[key].price;
  138. // 3. 计算商品总金额
  139. cart.totalMoney = cart.money.reduce(function (acc, cur) {
  140. return acc + cur;
  141. });
  142. // 4. 将数据渲染到指定元素上
  143. table.querySelector('.total').textContent = cart.total;
  144. table.querySelectorAll('.money')[key].textContent = cart.money[key];
  145. table.querySelector('.total-money').textContent = cart.totalMoney;
  146. };
  147. });
  148. //全选全不选功能
  149. // 选择标签
  150. let checkall = document.querySelector('.checkall')
  151. // console.log(checkall)
  152. let check = document.querySelectorAll('.check')
  153. // console.log(check)
  154. // 绑定事件
  155. checkall.onclick = function(){
  156. for(let i = 0; i < check.length; i++){
  157. check[i].checked = checkall.checked
  158. }
  159. }
  160. for(let i = 0; i < check.length; i++){
  161. check[i].onclick = function(){
  162. //只要有个选择框没有被选中,则全选框取消checked
  163. for(let j = 0; j < check.length; j++){
  164. if(check[j].checked == false){
  165. checkall.checked = false;
  166. return;
  167. }else {
  168. checkall.checked == true;
  169. }
  170. }
  171. }
  172. }
  173. </script>
  174. </body>
  175. </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