博客列表 >选项卡和购物车实例

选项卡和购物车实例

天宁
天宁原创
2022年04月10日 17:47:15365浏览

选项卡

代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>选项卡</title>
  8. <style>
  9. .box {
  10. width: 28em;
  11. display: grid;
  12. grid-template-columns: 3em 1fr;
  13. border-left: 1px solid #ccc;
  14. }
  15. .box ul {
  16. margin: 0;
  17. padding: 0;
  18. }
  19. .box li {
  20. list-style: none;
  21. /* height: 2em; */
  22. }
  23. .box li a {
  24. text-decoration: none;
  25. }
  26. .box li:hover {
  27. cursor: pointer;
  28. }
  29. .box .content {
  30. background-color: lightgreen;
  31. display: none;
  32. place-self: center start;
  33. }
  34. .box .content.active {
  35. display: block;
  36. }
  37. .box .menu li.active {
  38. background-color: lightgreen;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div class="box">
  44. <!-- 1. 标签 -->
  45. <!-- 子元素上的点击事件会冒泡到父元素,利用这个特点,只需要给父元素添加点击事件就可以了 -->
  46. <ul class="menu" onclick="show()">
  47. <!-- 先给默认显示的标签和对应的内容添加 class="active"处于激活状态/可见 -->
  48. <!-- 使用自定义属性data-index使标签和与之对应的内容进行绑定 -->
  49. <li data-index="1" class="active">本省</li>
  50. <li data-index="5">全国</li>
  51. <li data-index="8">防疫</li>
  52. </ul>
  53. <!-- 2. 内容 -->
  54. <ul class="content active" data-index="1">
  55. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  56. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  57. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  58. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  59. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  60. </ul>
  61. <ul class="content" data-index="5">
  62. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  63. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  64. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  65. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  66. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  67. </ul>
  68. <ul class="content" data-index="8">
  69. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  70. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  71. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  72. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  73. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  74. </ul>
  75. <ul class="content" data-index="8">
  76. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  77. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  78. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  79. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  80. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  81. </ul>
  82. </div>
  83. <script>
  84. function show() {
  85. // 1. 事件触发者(目标)
  86. let target = event.target;
  87. console.log('选择了:', target.textContent);
  88. //2.移除menu下所有li元素中为active的class
  89. [...target.parentNode.children].forEach(item => {
  90. item.classList.remove('active');
  91. });
  92. //3.添加active为class
  93. target.classList.add('active');
  94. //4.获取data-index属性
  95. let index = target.dataset.index;
  96. console.log('选中的下标:', index);
  97. //5.移除content下所有li元素中为active的class
  98. [...target.parentNode.parentNode.children].forEach(item => {
  99. item.classList.remove('active');
  100. });
  101. //6.为content下对应的li元素添加active
  102. let content = document.querySelectorAll('.content');
  103. [...content].find(item => item.dataset.index == index).classList.add('active');
  104. }
  105. </script>
  106. </body>
  107. </html>

购物车

代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>购物车</title>
  8. <style>
  9. .box {
  10. width: 22em;
  11. height: 2em;
  12. }
  13. .list > li {
  14. height: 1.6em;
  15. background-color: #efefef;
  16. display: grid;
  17. grid-template-columns: repeat(5, 3em);
  18. gap: 1em;
  19. place-items: center right;
  20. border-bottom: 1px solid #ccc;
  21. }
  22. .list > li:first-of-type {
  23. background-color: lightseagreen;
  24. color: white;
  25. }
  26. .list > li:hover:not(:first-of-type) {
  27. cursor: pointer;
  28. background-color: lightcyan;
  29. }
  30. .list > li input[type='number'] {
  31. width: 3em;
  32. border: none;
  33. outline: none;
  34. text-align: center;
  35. font-size: 1em;
  36. background-color: transparent;
  37. }
  38. .list > li:last-of-type span.total-num,
  39. .list > li:last-of-type span.total-amount {
  40. grid-column: span 2;
  41. place-self: center right;
  42. color: lightseagreen;
  43. }
  44. .account {
  45. float: right;
  46. background-color: lightseagreen;
  47. color: white;
  48. border: none;
  49. outline: none;
  50. width: 4.5em;
  51. height: 1.8em;
  52. }
  53. .account:hover {
  54. background-color: coral;
  55. cursor: pointer;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="box">
  61. <div class="selectAll">
  62. <!-- checkAll(): 当全选按钮的状态发生变化化,触发该事件 change -->
  63. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  64. <label for="check-all">全选</label>
  65. </div>
  66. <ul class="list" onchange="change()">
  67. <li>
  68. <span>选择</span>
  69. <span>品名</span>
  70. <span>数量</span>
  71. <span>单价</span>
  72. <span>金额</span>
  73. </li>
  74. <li>
  75. <input type="checkbox" checked />
  76. <span class="content">手机</span>
  77. <input type="number" value="1" min="1" class="num" />
  78. <span class="price">100</span>
  79. <span class="amount">0</span>
  80. </li>
  81. <li>
  82. <input type="checkbox" checked />
  83. <span class="content">电脑</span>
  84. <input type="number" value="2" min="1" class="num" />
  85. <span class="price">200</span>
  86. <span class="amount">0</span>
  87. </li>
  88. <li>
  89. <input type="checkbox" checked />
  90. <span class="content">相机</span>
  91. <input type="number" value="3" min="1" class="num" />
  92. <span class="price">300</span>
  93. <span class="amount">0</span>
  94. </li>
  95. <li>
  96. <span>总计:</span>
  97. <span class="total-num">0</span>
  98. <span class="total-amount">0</span>
  99. </li>
  100. </ul>
  101. <button class="account">结算</button>
  102. </div>
  103. <script>
  104. // 1. 全选
  105. function checkAll() {
  106. // 1. 全选按钮状态
  107. let status = event.target.checked;
  108. // 2. 根据状态动态设置所有商品的状态
  109. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  110. items.forEach(item => (item.checked = status));
  111. change();
  112. }
  113. //购物车发生改变时,计算总价和总数量,金额,并显示到页面上
  114. //使用事件冒泡来处理变化
  115. function change() {
  116. let target = event.target;
  117. console.log('选择了:', event.target);
  118. //1.当商品未全部选中时,全选按钮不选中
  119. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  120. document.querySelector('.selectAll input[type="checkbox"]').checked = [...items].every(
  121. item => item.checked
  122. );
  123. //2.获取所有的选中的商品
  124. let checkItems = document.querySelectorAll('.list li input[type="checkbox"]:checked');
  125. console.log(checkItems);
  126. //3.计算金额,总金额和总数量,并显示到页面上
  127. getAmount(checkItems);
  128. getTotalAmount(checkItems);
  129. getTotalNum(checkItems);
  130. }
  131. // 当页面加载的时候自动计算
  132. window.onload = change;
  133. //计算选中的总数量,并更新到页面上
  134. function getTotalNum(checkItems) {
  135. let totalNum = 0;
  136. checkItems.forEach(item => {
  137. totalNum += parseInt(item.nextElementSibling.nextElementSibling.value);
  138. });
  139. document.querySelector('.total-num').innerText = totalNum;
  140. }
  141. //计算选中的总金额,并更新到页面上
  142. function getTotalAmount(checkItems) {
  143. let totalAmount = 0;
  144. checkItems.forEach(item => {
  145. totalAmount +=
  146. parseInt(item.nextElementSibling.nextElementSibling.value) *
  147. parseInt(item.nextElementSibling.nextElementSibling.nextElementSibling.innerText);
  148. });
  149. document.querySelector('.total-amount').innerText = totalAmount;
  150. }
  151. //计算选中单个商品的金额并更新到页面上
  152. function getAmount(checkItems) {
  153. checkItems.forEach(item => {
  154. let num = item.parentNode.querySelector('.num').value;
  155. let price = item.parentNode.querySelector('.price').innerText;
  156. item.parentNode.querySelector('.amount').innerText = num * price;
  157. });
  158. }
  159. //把选中的商品的数量和金额更新到页面上,单个商品总金额也更新
  160. function updateTotalNumAndAmount() {
  161. let totalNum = getTotalNum(checkItems);
  162. let totalAmount = getTotalAmount(checkItems);
  163. getAmount(checkItems);
  164. document.querySelector('.total-num').innerText = totalNum;
  165. document.querySelector('.total-amount').innerText = totalAmount;
  166. }
  167. </script>
  168. </body>
  169. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议