Blogger Information
Blog 1
fans 0
comment 0
visits 276
Related recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车作业提交
手机用户0035810
Original
276 people have browsed it

购物车效果图:

代码如下:

  1. <script>
  2. function checkAll(){
  3. let status=document.querySelector('.check-all').checked;
  4. // console.log(status)
  5. let list=document.querySelectorAll('.list li input[type=checkbox]');
  6. // console.log(list)
  7. list.forEach(item=>item.checked=status)
  8. autoCalc()
  9. }
  10. function checkItems(){
  11. let list=document.querySelectorAll('.list li input[type=checkbox]');
  12. // console.log(list)
  13. let status=[...list].every(item=>item.checked)
  14. document.querySelector('.check-all').checked=status
  15. autoCalc()
  16. // checkStatus=[...list].map(arg1=>arg1.checked)
  17. // console.log(checkStatus)
  18. }
  19. // list=document.querySelectorAll('.list li input[type=checkbox]')
  20. //
  21. // let status=[...list][0].checked
  22. let nums=document.querySelectorAll('.num')
  23. // console.log(nums)
  24. function autoCalc(){
  25. //定义数量数组:
  26. let numArr= [...nums].map(num=> parseInt(num.value));
  27. console.log(numArr)
  28. console.log(numArr)
  29. // console.log(list)
  30. // console.log(numArr)
  31. //定义金额数组:
  32. let price=document.querySelectorAll('.price')
  33. // console.log(price)
  34. let priceArr=[...price].map(item=>parseInt(item.textContent))
  35. // console.log(priceArr)
  36. //在checkbox未选中的时候,在数据设置为0之前先计算单个商品总金额,:
  37. let amountArr1= numArr.map((item,index)=>item*priceArr[index])
  38. // console.log(amountArr)
  39. //在checkbox未选中的时候,把商品数量那个数组对应的值改为0
  40. let list=document.querySelectorAll('.list li input[type=checkbox]');
  41. checkStatus=[...list].map(arg1=>arg1.checked)
  42. checkStatus.map(function (arg1,index){
  43. if (arg1===false){
  44. numArr.splice(index,1,0)
  45. }
  46. }
  47. )
  48. //单个商品总金额数组
  49. let amountArr= numArr.map((item,index)=>item*priceArr[index])
  50. //计算总数量:
  51. function allNum(arg){
  52. return arg.reduce((arg1,arg2)=>arg1+arg2);
  53. }
  54. //计算总金额:
  55. function allPrice(arg){
  56. return arg.reduce((arg1,arg2)=>arg1+arg2)
  57. }
  58. //写入总数量到页面
  59. document.querySelector('.total-num').textContent=allNum(numArr)
  60. //写入总金额到页面
  61. document.querySelector('.total-amount').textContent=allNum(amountArr)
  62. //单个商品总金额
  63. document.querySelectorAll('.amount').forEach((arg1,arg2)=>arg1.textContent=amountArr1[arg2])
  64. }
  65. // 当页面加载的时候自动计算
  66. window.onload=autoCalc;
  67. //数量改变时自动计算:
  68. nums.forEach(num => (num.onchange=autoCalc))
  69. </script>
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