Blogger Information
Blog 52
fans 1
comment 1
visits 38786
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0412作业-购物车
小丑0o鱼
Original
521 people have browsed it

  1. css
  2. table {
  3. border-collapse: collapse;
  4. width: 90%;
  5. text-align: center;
  6. margin: auto;
  7. }
  8. table caption {
  9. margin-bottom: 15px;
  10. font-size: 1.5rem;
  11. }
  12. table th,
  13. table td {
  14. border-bottom: 1px solid #ccc;
  15. padding: 5px;
  16. font-weight: normal;
  17. }
  18. table thead tr:first-of-type {
  19. background-color: #e6e6e6;
  20. height: 3em;
  21. }
  22. table input[type="checkbox"] {
  23. width: 1.5em;
  24. height: 1.5em;
  25. }
  26. table tbody tr {
  27. border-bottom: 1px solid #ccc;
  28. }
  29. table tbody tr:hover {
  30. background-color: #f6f6f6;
  31. cursor: pointer;
  32. }
  33. tbody img {
  34. width: 3em;
  35. }
  36. tbody input[type="number"] {
  37. width: 3em;
  38. }
  39. button {
  40. width: 150px;
  41. height: 30px;
  42. outline: none;
  43. border: none;
  44. background-color: teal;
  45. color: white;
  46. letter-spacing: 5px;
  47. }
  48. button:hover {
  49. opacity: 0.7;
  50. cursor: pointer;
  51. }
  52. html
  53. <table>
  54. <caption>
  55. 购物车
  56. </caption>
  57. <thead>
  58. <tr>
  59. <!-- 全选复选框 -->
  60. <th><input type="checkbox" name="checkAll" id="check-all" /><label for="check-all">全选</label>
  61. </th>
  62. <th>图片</th>
  63. <th>品名</th>
  64. <th>单位</th>
  65. <th>单价/元</th>
  66. <th>数量</th>
  67. <th>金额/元</th>
  68. </tr>
  69. </thead>
  70. <tbody>
  71. <tr>
  72. <td>
  73. <input type="checkbox" name="item" value="SN-1020" />
  74. </td>
  75. <td>
  76. <a href=""><img src="images/p1.jpg" alt="" /></a>
  77. </td>
  78. <td>iPhone 11</td>
  79. <td>台</td>
  80. <td>4799</td>
  81. <td><input type="number" min="1" value="1" /></td>
  82. <td class="amount">0</td>
  83. </tr>
  84. <tr>
  85. <td>
  86. <input type="checkbox" name="item" value="SN-1020" />
  87. </td>
  88. <td>
  89. <a href=""><img src="images/p2.jpg" alt="" /></a>
  90. </td>
  91. <td>小米pro 11</td>
  92. <td>部</td>
  93. <td>3999</td>
  94. <td><input type="number" min="1" value="2" /></td>
  95. <td class="amount">0</td>
  96. </tr>
  97. <tr>
  98. <td>
  99. <input type="checkbox" name="item" value="SN-1030" />
  100. </td>
  101. <td>
  102. <a href=""><img src="images/p3.jpg" alt="" /></a>
  103. </td>
  104. <td>MacBook Pro</td>
  105. <td>台</td>
  106. <td>18999</td>
  107. <td><input type="number" min="1" value="1" /></td>
  108. <td class="amount">0</td>
  109. </tr>
  110. <tr>
  111. <td>
  112. <input type="checkbox" name="item" value="SN-1040" />
  113. </td>
  114. <td>
  115. <a href=""><img src="images/p4.jpg" alt="" /></a>
  116. </td>
  117. <td>小米75电视</td>
  118. <td>台</td>
  119. <td>5999</td>
  120. <td><input type="number" min="1" value="2" /></td>
  121. <td class="amount">0</td>
  122. </tr>
  123. <tr>
  124. <td>
  125. <input type="checkbox" name="item" value="SN-1050" />
  126. </td>
  127. <td>
  128. <a href=""><img src="images/p5.jpg" alt="" /></a>
  129. </td>
  130. <td>Canon 90D单反</td>
  131. <td>台</td>
  132. <td>9699</td>
  133. <td><input type="number" min="1" value="1" /></td>
  134. <td class="amount">0</td>
  135. </tr>
  136. </tbody>
  137. <tfoot>
  138. <tr style="font-weight: bolder; font-size: 1.2em">
  139. <td colspan="5">已选总计:</td>
  140. <td id="sum">0</td>
  141. <td id="total-amount">0</td>
  142. </tr>
  143. </tfoot>
  144. </table>
  145. <div style="width: 90%; margin: 10px auto">
  146. <button style="float: right; width: 100px">结算</button>
  147. </div>
  148. js
  149. // 全选ele
  150. const checkAll = document.querySelector('#check-all');
  151. // 单选商品ele
  152. const checkItems = document.querySelectorAll('input[name="item"]');
  153. // 单商品价格ele
  154. const price = document.querySelectorAll('tr > td:nth-of-type(5)');
  155. // 单商品个数ele
  156. const itemNum = document.querySelectorAll('input[type="number"]');
  157. // 单商品总价ele
  158. const itemAmount = document.querySelectorAll('.amount');
  159. // 全总价ele
  160. const totalAmount = document.querySelector('#total-amount');
  161. // 全商品总数ele
  162. const itemTotal = document.querySelector('#sum');
  163. // 定义数组相乘函数
  164. function resum(arr1, arr2) {
  165. return [arr1, arr2].reduce((pre, cur) => pre.map((item, index) => item * cur[index]));
  166. }
  167. // 所有变化函数
  168. function allChange() {
  169. // 商品是否选择数组
  170. let checkArr = [...checkItems].map((item) => item.checked * 1);
  171. // 商品数量数组
  172. let numberArr = [...itemNum].map((item) => item.value * 1);
  173. // 商品价格数组
  174. let priceArr = [...price].map((item) => item.textContent * 1);
  175. // 单商品总价数组
  176. let itemAmountArr = resum(priceArr, numberArr);
  177. // 单商品总价写入ele
  178. itemAmount.forEach((item, index) => item.textContent = itemAmountArr[index]);
  179. // 选定商品总数写入ele
  180. itemTotal.textContent = resum(numberArr, checkArr).reduce((pre, cur) => pre + cur);
  181. // 选定商品总价写入ele
  182. totalAmount.textContent = resum(itemAmountArr, checkArr).reduce((pre, cur) => pre + cur);
  183. }
  184. // 全选事件
  185. checkAll.onchange = ((ev) => {
  186. checkItems.forEach((item) => { item.checked = checkAll.checked });
  187. allChange();
  188. });
  189. // 单选事件
  190. checkItems.forEach((item) => {
  191. item.onchange = (ev) => {
  192. checkAll.checked = [...checkItems].every((item) => item.checked);
  193. allChange();
  194. }
  195. })
  196. // 数量变化事件
  197. itemNum.forEach((item, index) => {
  198. item.onchange = (ev) => {
  199. // 自动选择
  200. checkItems[index].checked = true;
  201. allChange();
  202. };
  203. })
  204. // 加载时
  205. window.onload = (() => allChange());
Correction status:Uncorrected

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