仿写购物车项目

仿写购物车项目
import carts from “./carts.js”const items = document.querySelectorAll(.carts-body .item)const total = document.querySelector(.total)const totalMoney = document.querySelector(.total-money) items.forEach((item, index)=>{ item.querySelector(.id).textContent = carts.data[index].id item.querySelector(.name).textCo系统开发mhkj33ntent= carts.data[index].name item.querySelector(.unit).textContent = carts.data[index].unit item.querySelector(.price).textContent = carts.data[index].price item.querySelector(.num).value = carts.data[index].num item.querySelector(.money).textContent = carts.data[index].money}) total.textContent = carts.totaltotal系统开发mhkj33Money.textContent = carts.totalMoneyconst prices = document.querySelectorAll(.carts-body .price)const nums = document.querySelectorAll(.carts-body .num)const monies = document.querySelectorAll(.carts-body .money)const checks = document.querySelectorAll(.carts-body .check)// forEach(在遍历的数组,数组的索引,数组) nums.forEach((n系统开发mhkj33um, index, arr)=>{ num.onchange =()=>{if(!checks[index].checked)returnfalse monies[index].textContent = prices[index].textContent * num.valueconst numArr =[…arr].map((item)=> parseInt(item.value)) total.textContent = numArr.reduce((pre, cur)=>pre + cur)const moneyArr =[…monies].map((item)=> parseFloat(item.textConte系统开发mhkj33nt)) totalMoney.textContent = moneyArr.reduce((pre, cur)=>pre + cur)}})const checkAll = document.querySelector(.check-all) let totalTmp = total.textContent let totalMoneyTmp = totalMoney.textContent checkAll.onchange =()=>{ checks.forEach((check)=>check.checked = checkAll.checked)if(checkAll.checked ==false){let itemSta系统开发mhkj33tus=[…checks].every(item=>item.checked ==false)if(itemStatus ==true){ total.textContent =0 totalMoney.textContent =0}}else{ total.textContent = totalTmp totalMoney.textContent = totalMoneyTmp}} checks.forEach((check, index)=>{ check.onchange =()=>{ checkAll.checked =[…checks].every(item=>item.checked)if(check.che系统开发mhkj33cked==false){ total.textContent -= nums[index].value totalMoney.textContent -= monies[index].textContent}else{ total.textContent = total.textContent *1+ nums[index].value *1 totalMoney.textContent = totalMoney.textContent *1+ monies[index].textContent *1}}})

总结:

把购物车项目js部分边看边写了一遍,把所有函数都变为箭头函数,简化代码,条理也很清楚;投票项目中把count赋值改系统开发mhkj33为count = data[index]就可以把原始赋值带入;计划后续有时间自己再完全重写一遍购物车项目,包括CSS、html和js部分全部重写。

批改老师:PHPz 批改状态:合格 老师批语:
作者最新博文
2023-03-08 14:59:36

© 版权声明
THE END
喜欢就支持一下吧
点赞450 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容