博客列表 >js购物车与JSON常用的二个API

js购物车与JSON常用的二个API

喜
原创
2022年08月02日 18:10:40333浏览

1. js购物车

  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>Document</title>
  8. </head>
  9. <style>
  10. *{
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. list-style: none;
  15. }
  16. img{
  17. width: 80px;
  18. height: 80px;
  19. }
  20. body{
  21. font-size: 14px;
  22. color: #666;
  23. }
  24. a{
  25. color: #666;
  26. text-decoration: none;}
  27. i{
  28. font-style: normal;
  29. }
  30. .box{
  31. width: 800px;
  32. margin: 0 auto;
  33. }
  34. ul{
  35. display: grid;
  36. grid-template-rows: 51px 1fr
  37. }
  38. li{
  39. display: grid;
  40. grid-template-columns: 107px 208px 79px 255px 79px 1fr;
  41. place-items: center;
  42. border-bottom: 1px dashed #ccc;
  43. }
  44. li:nth-of-type(n+2){
  45. height: 103px;
  46. }
  47. li:first-of-type{
  48. font-family: 宋体;
  49. }
  50. li .commodity{
  51. height: 100%;
  52. position: relative;
  53. }
  54. li p{
  55. position: absolute;
  56. bottom: 10px;
  57. left: 50%;
  58. transform: translateX(-50%);
  59. }
  60. li .inp{
  61. width: 54px;
  62. height: 22px;
  63. border: 1px solid rgb(118, 118, 118);
  64. text-align: center;
  65. }
  66. .btn{
  67. font-size: 0;
  68. }
  69. .btn button{
  70. width: 22px;
  71. height: 22px;
  72. border: 1px solid #ccc;
  73. background: none;
  74. outline: none;
  75. cursor: pointer;
  76. }
  77. .foot{
  78. display: flex;
  79. justify-content: space-between;
  80. align-items: center;
  81. width: 800px;
  82. margin: 10px auto;
  83. border: 1px solid #ccc;
  84. height: 50px;
  85. padding-left: 10px;
  86. }
  87. .left a{
  88. height: 48px;
  89. line-height: 48px;
  90. display: inline-block;
  91. margin-right: 50px;
  92. }
  93. .right{
  94. display: flex;
  95. align-items: center;
  96. }
  97. .right button{
  98. width: 80px;
  99. height: 54px;
  100. background: red;
  101. font: bold 20px/54px '宋体';
  102. color: #fff;
  103. text-align: center;
  104. border: none;
  105. cursor: pointer;
  106. }
  107. .right p{
  108. margin-right: 20px;
  109. }
  110. .right span{
  111. font-size: 14px;
  112. color: #ff0000;
  113. }
  114. .right span:last-of-type{
  115. font-weight: bold;
  116. }
  117. input{
  118. outline: none;
  119. }
  120. </style>
  121. <body>
  122. <div class="box">
  123. <ul>
  124. <li>
  125. <div class="all">
  126. <input type="checkbox"><span class="quan">全选</span>
  127. </div>
  128. <span>商品</span>
  129. <span>单价</span>
  130. <span>商品数量</span>
  131. <span>小计</span>
  132. <span>操作</span>
  133. </li>
  134. <li>
  135. <input type="checkbox">
  136. <div class="commodity"> <img src="https://img2.baidu.com/it/u=1017090698,4049749381&fm=253&fmt=auto&app=138&f=PNG?w=504&h=500" alt=""><p>牛奶</p></div>
  137. <span><i class="danjia">5</i>¥</span>
  138. <div class="btn">
  139. <button disabled class="jian">-</button>
  140. <input type="text" class="inp quantity" value="1" readonly>>
  141. <button class="jia">+</button>
  142. </div>
  143. <span><i class="xiaoji">0</i>¥</span>
  144. <a href="#" class="del">删除</a>
  145. </li>
  146. <li>
  147. <input type="checkbox">
  148. <div class="commodity"> <img src="https://img2.baidu.com/it/u=1017090698,4049749381&fm=253&fmt=auto&app=138&f=PNG?w=504&h=500" alt=""><p>牛奶</p></div>
  149. <span><i class="danjia">10</i>¥</span>
  150. <div class="btn">
  151. <button disabled class="jian">-</button>
  152. <input type="text" class="inp quantity" value="1" readonly>>
  153. <button class="jia">+</button>
  154. </div>
  155. <span><i class="xiaoji">0</i>¥</span>
  156. <a href="#" class="del">删除</a>
  157. </li>
  158. <li>
  159. <input type="checkbox">
  160. <div class="commodity"> <img src="https://img2.baidu.com/it/u=1017090698,4049749381&fm=253&fmt=auto&app=138&f=PNG?w=504&h=500" alt=""><p>牛奶</p></div>
  161. <span><i class="danjia">20</i>¥</span>
  162. <div class="btn">
  163. <button disabled class="jian">-</button>
  164. <input type="text" class="inp quantity" value="1" readonly>>
  165. <button class="jia">+</button>
  166. </div>
  167. <span><i class="xiaoji">0</i>¥</span>
  168. <a href="#" class="del">删除</a>
  169. </li>
  170. <li>
  171. <input type="checkbox">
  172. <div class="commodity"> <img src="https://img2.baidu.com/it/u=1017090698,4049749381&fm=253&fmt=auto&app=138&f=PNG?w=504&h=500" alt=""><p>牛奶</p></div>
  173. <span><i class="danjia">35</i>¥</span>
  174. <div class="btn">
  175. <button disabled class="jian">-</button>
  176. <input type="text" class="inp quantity" value="1" readonly>
  177. <button class="jia">+</button>
  178. </div>
  179. <span><i class="xiaoji">0</i>¥</span>
  180. <a href="#" class="del">删除</a>
  181. </li>
  182. </ul>
  183. </div>
  184. <div class="foot">
  185. <div class="left">
  186. <a href="#" class="sx">删除所选商品</a>
  187. <a href="#" class="qk">清理购物车</a>
  188. </div>
  189. <div class="right">
  190. <p>已经选中<span class="shul">0</span>件商品;总价:<span><i class="zonjia">0</i>¥</span></p>
  191. <button>去结算</button>
  192. </div>
  193. </div>
  194. <script>
  195. // 全选模块
  196. let select=document.querySelector('.all input[type="checkbox"]')
  197. let item=Array.from(document.querySelectorAll('li:nth-of-type(n+2) input[type="checkbox"]'))
  198. select.addEventListener('click',()=>item.forEach(i=>i.checked=select.checked))
  199. item.forEach(i=>i.addEventListener('click',()=>select.checked=item.every(i=>i.checked===true)))
  200. let jia=[...document.querySelectorAll('button.jia')]
  201. let jian=[...document.querySelectorAll('button.jian')]
  202. let shul=[...document.querySelectorAll('.inp.quantity')]
  203. let xiaoji=[...document.querySelectorAll('i.xiaoji')]
  204. // 加号模块
  205. jia.forEach((i,o)=>i.addEventListener('click',()=>{
  206. if(item[o].checked){
  207. jian[o].disabled=false
  208. shul[o].value++
  209. xiaoji[o].innerHTML=document.querySelectorAll('i.danjia')[o].innerHTML*shul[o].value
  210. getShul()
  211. getZonjia()
  212. }else{
  213. alert('请选择商品')
  214. return false
  215. }}))
  216. // 减号模块
  217. jian.forEach((i,o)=>i.addEventListener('click',()=>{
  218. if(item[o].checked){
  219. shul[o].value--
  220. if(shul[o].value<=1)jian[o].disabled=true
  221. xiaoji[o].innerHTML=document.querySelectorAll('i.danjia')[o].innerHTML*shul[o].value
  222. getShul()
  223. getZonjia()
  224. }else{
  225. alert('请选择商品')
  226. return false
  227. }}))
  228. // 总数量模块
  229. function getShul(){
  230. document.querySelector('span.shul').innerHTML= shul.map(i=>+i.value).reduce((a,b)=>a+b)
  231. }
  232. // 总价模块
  233. function getZonjia(){
  234. document.querySelector('i.zonjia').innerHTML= xiaoji.map(i=>+i.innerHTML).reduce((a,b)=>a+b)
  235. }
  236. // 右边删除模块
  237. let li=[...document.querySelectorAll('li')]
  238. document.querySelectorAll('a.del').forEach((i,o)=>{i.addEventListener('click',()=>{
  239. if(confirm('确认删除吗')) li[o+1].remove()
  240. })})
  241. // 删除所选商品模块
  242. document.querySelector('a.sx').addEventListener('click',()=>{
  243. item.forEach((i,o)=>{if(i.checked)li[o+1].remove()})})
  244. // 清理购物车模块
  245. document.querySelector('a.qk').addEventListener('click',()=>{
  246. if(confirm('确认全部删除吗')) li.splice(1).map(i=>i.outerHTML=null)
  247. })
  248. // 当页面加载时,根据默认值进行自动计算
  249. window.addEventListener('load',function(){
  250. xiaoji.forEach((i,o)=>{xiaoji[o].innerHTML=document.querySelectorAll('i.danjia')[o].innerHTML*shul[o].value})
  251. getShul()
  252. getZonjia()
  253. })
  254. </script>
  255. </body>
  256. </html>

2.json常用的二个API

  1. 当数据在浏览器与服务器之间进行交换时,这些数据只能是文本。
  2. JSON 属于文本,并且我们能够把任何 JavaScript 对象转换为 JSON,然后将 JSON 发送到服务器。
  3. 我们也能把从服务器接收到的任何 JSON 转换为 JavaScript 对象。
  4. 以这样的方式,我们能够把数据作为 JavaScript 对象来处理,无需复杂的解析和转译。
  1. <script>
  2. // 1. 前端 -> 后端: js对象序列化 JSON.stringify()-> json字符串
  3. let arr=[1,2,3,true]
  4. let obj={name:1,age:21}
  5. localStorage.setItem('obj',JSON.stringify(obj))
  6. // 2. 后端 -> 前端, json反序列化 JSON.parse() ->
  7. js对象
  8. console.log(JSON.parse(localStorage.getItem('obj')));
  9. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议