博客列表 >javascript-基础(七)双色球、选项卡、购物车

javascript-基础(七)双色球、选项卡、购物车

CY明月归
CY明月归原创
2022年04月11日 09:17:10845浏览

1. 双色球

  1. //定义需要的数组
  2. redarrs = [];
  3. redarr = [];
  4. bluearrs = [];
  5. winarr = [];
  6. //生成红球、蓝球
  7. for (i = 0; i < 33; i++) {
  8. balls = i + 1;
  9. redarrs.push(balls);
  10. }
  11. for (i = 0; i < 16; i++) {
  12. balls = i + 1;
  13. bluearrs.push(balls);
  14. }
  15. //页面展示信息
  16. redlist = document.querySelector(".redlist");
  17. redlist.textContent += redarrs;
  18. bluelist = document.querySelector(".bluelist");
  19. bluelist.textContent += bluearrs;
  20. for (i = 0; i < 6; i++) {
  21. //随机获得下标
  22. index = Math.floor(Math.random() * redarrs.length);
  23. //取值
  24. a = redarrs[index];
  25. //压入数组redarr
  26. redarr.push(a);
  27. //删除已压入的数据(下标,个数)
  28. redarrs.splice(index, 1);
  29. }
  30. blueball = Math.floor(Math.random() * bluearrs.length + 1);
  31. winarr.push(`红球:${redarr.sort((a, b) => a - b)}`);
  32. winarr.push(`篮球:${blueball}`);
  33. winbox = document.querySelector(".winlist1");
  34. redarr.forEach((item) => {
  35. winball = document.createElement("div");
  36. winball.textContent = item;
  37. winbox.append(winball);
  38. });
  39. //追加蓝球到winarr
  40. b = document.createElement("div");
  41. b.textContent = blueball;
  42. winbox.append(b);
  43. winlist = document.querySelector(".winlist");
  44. winlist.textContent = "机选号码:" + winarr;
  45. //彩色红球、蓝球信息
  46. reds = document.querySelector(".red");
  47. redarrs.forEach((item) => {
  48. redons = document.createElement("div");
  49. redons.textContent = item;
  50. reds.append(redons);
  51. });
  52. blues = document.querySelector(".blue");
  53. bluearrs.forEach((item) => {
  54. blueons = document.createElement("div");
  55. blueons.textContent = item;
  56. blues.append(blueons);
  57. });
  58. //获取选中红球
  59. redarr.forEach((i) => {
  60. index = i - 1;
  61. chooseone = document.querySelectorAll(".red div")[index];
  62. chooseone.style.color = "white";
  63. chooseone.style.backgroundColor = "red";
  64. });
  65. //获取选中蓝球
  66. choosetwo = document.querySelectorAll(".blue div")[blueball - 1];
  67. choosetwo.style.color = "white";
  68. choosetwo.style.backgroundColor = "blue";

2.选项卡

  1. function show() {
  2. // 事件绑定者
  3. ul = event.currentTarget
  4. // 事件主体
  5. li = event.target
  6. // console.log(ul,li)
  7. //转换数组[...ul.children] 移除active
  8. Array.from(ul.children).forEach(i=>i.classList.remove('active'))
  9. li.classList.add('active')
  10. //移除active
  11. let content = document.querySelectorAll('.content')
  12. //console.log(content)
  13. content.forEach(i => i.classList.remove('active'))
  14. //绑定nav与列表
  15. Array.from(content).find(ul=>ul.dataset.index === li.dataset.index).classList.add('active')
  16. }
  17. .box .content{
  18. margin: .625rem;
  19. display: none;
  20. padding: 1.875rem;
  21. border-radius: .625rem;
  22. }
  23. .box .active{
  24. color: white;
  25. display: inline-block;
  26. background-color: rgb(23, 196, 152);
  27. }

3.购物车: 只计算选中商品,并同时更新相关数据

  1. function autoCalculate() {
  2. let calc = document.querySelectorAll('.content input[type="checkbox"]');
  3. arr1 = Array.from(calc).map((i) => i.checked == true);
  4. arr2 = arr1.map((i) => Number(i));
  5. arr3 = [];
  6. arr2.forEach((i, index) => {
  7. const numArr = [...nums].map((num) => parseInt(num.value));
  8. arr3.push(i * numArr[index]);
  9. });
  10. // 商品数量数组
  11. const numArr = arr3;
  12. // 只计算选中商品数量更新时更新相关数据
  13. // 商品数量数组
  14. const itemNumArr = [...nums].map((num) => parseInt(num.value));
  15. console.log(numArr);
  16. // 单价数组
  17. const prices = document.querySelectorAll(".price");
  18. const priceArr = [...prices].map((price) => parseInt(price.textContent));
  19. console.log(priceArr);
  20. // 商品金额数组
  21. const itemAmountArr = getAmount(itemNumArr, priceArr);
  22. console.log(itemAmountArr);
  23. // 选中商品金额数组
  24. const amountArr = getAmount(numArr, priceArr);
  25. console.log(amountArr);
  26. // 为每个商品填充金额
  27. document
  28. .querySelectorAll(".totalprice")
  29. .forEach((amount, index) => (amount.textContent = itemAmountArr[index]));
  30. // 选中商品总数量
  31. console.log(getTotalNum(numArr));
  32. document.querySelector(".nums").textContent = getTotalNum(numArr);
  33. // 选中商品总金额
  34. document.querySelector(".allitemprice").textContent = getTotalAmount(
  35. amountArr
  36. );
  37. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议