Blogger Information
Blog 28
fans 0
comment 0
visits 15878
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
07-16作业
︷肉園耔︷
Original
498 people have browsed it
  1. //功能全选和全不选的功能
  2. //1.全选复选框
  3. const checkAll =document.querySelector('#check-all');
  4. //2.每个商品的复选框
  5. const checkItems =document.getElementsByName("item");
  6. //change 当控件的值发生变量的时候 触发它
  7. //console.log(checkAll.checked);
  8. //3.每个商品状态必须和全选复选框同步变化
  9. checkAll.onchange = (ev)=>
  10. checkItems.forEach(((item)=>
  11. item.checked= ev.target.checked
  12. ));
  13. //4.根据每个商品的复选框状态,动态设置全选框的状态
  14. //checkITems.forEach(item=>item.onchange=()=>chenckIT)
  15. checkItems.forEach(function(item){
  16. item.onchange=function(){
  17. let res=[...checkItems].every(function(item){
  18. return item.checked;
  19. });
  20. checkAll.checked=res;
  21. }
  22. })
  23. //简化版
  24. //checkItems.forEach(item=>(item.onchange=()=>(checkAll.checked=[...checkItems].every(item=>))))
  25. //功能二:自动计算
  26. //功能:分析所有的计算,都是基于“数量的变化",第一步就要获取所有商品的数量控件
  27. const numInput= document.querySelectorAll('input[type="number"]');
  28. // console.log(numInput);
  29. //给一个数量控件绑定一事件
  30. //当控件的值发生变化的是Hi好,自动进行重新计算
  31. numInput.forEach(input=>(input.onchange=autoCalculate));
  32. function autoCalculate(){
  33. //第一步 获取每个商品的金额=数量 * 单价
  34. //数量 ,当前有多个商品,所以呀应该返回有数组成的集合
  35. const numbers = document.querySelectorAll('input[type="number"]');
  36. console.log(numbers);
  37. console.log([...numbers]);
  38. // [...numbers].forEach(num=>console.log(typeof parseInt(num.value)));
  39. //[...numbers].forEach(num=>console.log(num.value * 1));
  40. const numArr =[...numbers].map (num=>num.value * 1);
  41. console.log(numArr);
  42. //获取单价组成的数组
  43. const prices = document.querySelectorAll("tbody .price");
  44. console.log(prices);
  45. //const numbers =document.querySelectorAll('input[type="number]');
  46. const priceArr=[...prices].map(price=>price.textContent * 1);
  47. console.log(numArr,priceArr);
  48. const amountArr= [priceArr,numArr].reduce((prve,curr)=>prve.map((item,key)=>item*curr[key]));
  49. console.log(amountArr);
  50. //第二步 商品总数
  51. let sum=numArr.reduce((prev,curr)=>prev+curr);
  52. //第三步 所有的商品总金额
  53. let total =amountArr.reduce((prev,curr)=>prev+curr);
  54. //第四步 讲以上计算的结果,渲染到页面上
  55. document.querySelectORAll('.amount').forEache((item,index)=>(item.textContent=amountArr[index]));
  56. document.querySelector("#sum").textContent=sum;
  57. document.querySelector("#total-amount").textContent=total;
  58. }
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