Blogger Information
Blog 12
fans 0
comment 0
visits 6744
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车效果实现
番茄炒蛋
Original
502 people have browsed it

HTML部分

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="style.css" />
  7. </head>
  8. <body>
  9. <div id="box">
  10. <table>
  11. <thead>
  12. <tr>
  13. <td><input type="checkbox" id="cheAll">全选</td>
  14. <td>图片</td>
  15. <td class="active">品名</td>
  16. <td>单位</td>
  17. <td>单价/元</td>
  18. <td>数量</td>
  19. <td>金额/元</td>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td><input type="checkbox" class="item"></td>
  25. <td><img src="img/p1.png"></td>
  26. <td>JavaScript权威指南(第七版)</td>
  27. <td></td>
  28. <td class="count">100</td>
  29. <td><input type="number" min="1" value="1" name="" id="" value="" /></td>
  30. <td class="prcie">0</td>
  31. </tr>
  32. <tr>
  33. <td><input type="checkbox" class="item"></td>
  34. <td><img src="img/p2.png"></td>
  35. <td>JavaScript高级程序设计(第四版)</td>
  36. <td></td>
  37. <td class="count">129</td>
  38. <td><input type="number" min="1" value="1" name="" id="" value="" /></td>
  39. <td class="prcie">0</td>
  40. </tr>
  41. <tr>
  42. <td><input type="checkbox" class="item"></td>
  43. <td><img src="img/p3.png"></td>
  44. <td>JavaScript忍者秘籍(第二版)</td>
  45. <td></td>
  46. <td class="count">99</td>
  47. <td><input type="number" min="1" value="1" name="" id="" value="" /></td>
  48. <td class="prcie">0</td>
  49. </tr>
  50. <tr>
  51. <td><input type="checkbox" class="item"></td>
  52. <td><img src="img/p5.png"></td>
  53. <td>MacBook Pro 16 九代17 16G 512G</td>
  54. <td></td>
  55. <td class="count">20000</td>
  56. <td><input type="number" min="1" value="1" name="" id="" value="" /></td>
  57. <td class="prcie">0</td>
  58. </tr>
  59. <tr>
  60. <td><input type="checkbox" class="item"></td>
  61. <td><img src="img/p4.png"></td>
  62. <td>华硕飞行堡垒9</td>
  63. <td></td>
  64. <td class="count">5999</td>
  65. <td><input type="number" min="1" value="1" name="" id="" value="" /></td>
  66. <td class="prcie">0</td>
  67. </tr>
  68. </tbody>
  69. <tfoot>
  70. <tr>
  71. <td colspan="5">
  72. 总计:
  73. </td>
  74. <td class="quantity">5</td>
  75. <td class="total">22222</td>
  76. </tr>
  77. </tfoot>
  78. </table>
  79. </div>
  80. <script src="index.js" type="text/javascript" charset="utf-8"></script>
  81. </body>
  82. </html>

JS部分

实现全选和全选自动选中部分

  1. // 获取全选按钮
  2. const allBtn = document.querySelector("#cheAll");
  3. // 获取所有单选按钮
  4. const items = document.querySelectorAll(".item");
  5. allBtn.onchange = function(e) {
  6. items.forEach((item) => {
  7. item.checked = allBtn.checked;
  8. })
  9. }
  10. items.forEach((item) => {
  11. item.onchange = () => {
  12. let res = [...items].every((retu) => {
  13. return retu.checked;
  14. })
  15. allBtn.checked = res;
  16. }
  17. })

价格、数量、总金额效果实现

  1. window.onload = autoefreshPrice();
  2. // 计算价格
  3. const Nums = document.querySelectorAll("input[type='number']");
  4. Nums.forEach(item => item.onchange = autoefreshPrice);
  5. function autoefreshPrice() {
  6. // 金额 = 数量 * 单价
  7. const numbers = document.querySelectorAll("input[type='number']");
  8. // 获取数量
  9. let newNumber = [...numbers].map(num => num.value * 1);
  10. // 获取单价
  11. let counts = document.querySelectorAll(".count");
  12. let newCounts = [...counts].map(counts => counts.textContent * 1);
  13. // 金额
  14. let NumberCounts = [newNumber, newCounts].reduce((prev, next) => prev.map((a, key) => {
  15. return a * newCounts[key];
  16. }))
  17. // 商品总数
  18. let quantitys = newNumber.reduce((prev, next) => prev + next);
  19. let price = document.querySelectorAll(".prcie");
  20. let quantity = document.querySelector(".quantity");
  21. let total = document.querySelector(".total");
  22. // 渲染到页面
  23. // 金额
  24. price.forEach((pr, index) => pr.textContent = NumberCounts[index]);
  25. // 总计
  26. quantity.textContent = quantitys;
  27. // 总金额
  28. total.textContent = NumberCounts.reduce((prev, next) => prev + next)
  29. }

Correcting teacher:天蓬老师天蓬老师

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