Blogger Information
Blog 48
fans 0
comment 0
visits 34356
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS的案列简单演示(0821)
丶久而旧之丶
Original
1034 people have browsed it

案列

使用jQuery实现功能

一键换肤

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>换肤</title>
  7. <style>
  8. .container {
  9. width: 100px;
  10. display: flex;
  11. box-sizing: border-box;
  12. }
  13. .container>img {
  14. width: 100%;
  15. margin: 0 3px 0 0;
  16. border: 3px solid white;
  17. opacity: 0.6;
  18. }
  19. .container>img:hover {
  20. opacity: 1;
  21. cursor: pointer;
  22. width: 110%;
  23. }
  24. html,
  25. body {
  26. height: 100%;
  27. }
  28. body {
  29. background-image: url("imgs/03.jpg");
  30. background-repeat: no-repeat;
  31. background-size: 100% 100%;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div class="container">
  37. <img src="imgs/03.jpg" alt="">
  38. <img src="imgs/06.jpg" alt="">
  39. <img src="imgs/07.jpg" alt="">
  40. </div>
  41. </body>
  42. <script src="../lib/jquery-3.5.1.js"></script>
  43. <script>
  44. $("div").click(function (ev) {
  45. $("body").css("background-image", "url(" + ev.target.src + ")");
  46. })
  47. </script>
  48. </html>

全选和自动计算金额

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>自动计算价格</title>
  7. <style>
  8. table {
  9. border: 1px solid;
  10. border-collapse: collapse;
  11. margin: 20px auto;
  12. width: 500px;
  13. text-align: center;
  14. }
  15. tr td {
  16. border: 1px solid;
  17. }
  18. caption {
  19. margin-bottom: 10px;
  20. }
  21. div {
  22. width: 500px;
  23. margin: auto;
  24. }
  25. thead {
  26. background-color: aquamarine;
  27. }
  28. button {
  29. float: right;
  30. width: 80px;
  31. }
  32. .tain {
  33. box-sizing: border-box;
  34. width: 50px;
  35. text-align: center;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <table>
  41. <caption>购物车</caption>
  42. <thead>
  43. <tr>
  44. <td><input type="checkbox" name="itemAll" id="itemAll" checked>全选</td>
  45. <td>ID</td>
  46. <td>品名</td>
  47. <td>单位</td>
  48. <td>单价/元</td>
  49. <td>数量</td>
  50. <td>金额/元</td>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <tr>
  55. <td><input type="checkbox" name="itemID" id="itemID" value="SN-1010" checked></td>
  56. <td>SN-1010</td>
  57. <td>MacBook Pro电脑</td>
  58. <td></td>
  59. <td>18999</td>
  60. <td>
  61. <input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
  62. </td>
  63. <td></td>
  64. </tr>
  65. <tr>
  66. <td><input type="checkbox" name="itemID" id="itemID" value="SN-1020" checked></td>
  67. <td>SN-1020</td>
  68. <td>iPhone手机</td>
  69. <td></td>
  70. <td>4999</td>
  71. <td>
  72. <input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
  73. </td>
  74. <td></td>
  75. </tr>
  76. <tr>
  77. <td><input type="checkbox" name="itemID" id="itemID" value="SN-1030" checked></td>
  78. <td>SN-1030</td>
  79. <td>智能Ai音箱</td>
  80. <td></td>
  81. <td>399</td>
  82. <td>
  83. <input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
  84. </td>
  85. <td></td>
  86. </tr>
  87. <tr>
  88. <td><input type="checkbox" name="itemID" id="itemID" value="SN-1040" checked></td>
  89. <td>SN-1040</td>
  90. <td>SSD移动硬盘</td>
  91. <td></td>
  92. <td>888</td>
  93. <td>
  94. <input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
  95. </td>
  96. <td></td>
  97. </tr>
  98. <tr>
  99. <td><input type="checkbox" name="itemID" id="itemID" value="SN-1050" checked></td>
  100. <td>SN-1050</td>
  101. <td>黄山毛峰</td>
  102. <td></td>
  103. <td>999</td>
  104. <td>
  105. <input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
  106. </td>
  107. <td></td>
  108. </tr>
  109. <tr>
  110. <td colspan="5">总计</td>
  111. <td id="totalNum"></td>
  112. <td id="totalPrice"></td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. <div>
  117. <button>结算</button>
  118. </div>
  119. <script src="../lib/jquery-3.5.1.js"></script>
  120. <script>
  121. $("#itemAll").on("change", chend);
  122. function chend(ev) {
  123. $("input[name='itemID']").prop("checked", ev.target.checked);
  124. if ((ev.target.checked)) {
  125. $("input[type='number']").prop("value", 1);
  126. } else {
  127. $("input[type='number']").prop("value", 0);
  128. }
  129. // 获取商品数量
  130. Counts = getCounts(numbers);
  131. // 获取商品单价
  132. Prices = getPrices(Price);
  133. // 获取商品金额
  134. Amouts = getAmouts(Counts, Prices);
  135. // 获取商品总数量
  136. totalNum = num(Counts);
  137. // 获取商品总金额
  138. totalPrice = toPrice(Amouts);
  139. // 渲染商品金额到页面中
  140. $("tbody>tr>td:last-of-type").each(function (key, value) {
  141. $(value).html(Amouts[key]);
  142. });
  143. // 渲染商品总数量到页面中
  144. $("#totalNum").html(totalNum);
  145. // 渲染商品总金额到页面中
  146. $("#totalPrice").html(totalPrice);
  147. }
  148. $("input[name='itemID']").change(function (ev) {
  149. var count = 0;
  150. // 循环按钮选中状态的个数
  151. $("input[name='itemID']").each(function (key, value) {
  152. if (value.checked) count++;
  153. });
  154. if (ev.target.checked) {
  155. $(ev.target).parent().siblings().eq(4).children().prop("value", 1);
  156. } else {
  157. $(ev.target).parent().siblings().eq(4).children().prop("value", 0);
  158. }
  159. // 根据个数判断全选按钮是否要选ziyu
  160. $("#itemAll").prop("checked", (count === $("input[name='itemID']").length));
  161. // 获取商品数量
  162. Counts = getCounts(numbers);
  163. // 获取商品单价
  164. Prices = getPrices(Price);
  165. // 获取商品金额
  166. Amouts = getAmouts(Counts, Prices);
  167. // 获取商品总数量
  168. totalNum = num(Counts);
  169. // 获取商品总金额
  170. totalPrice = toPrice(Amouts);
  171. // 渲染商品金额到页面中
  172. $("tbody>tr>td:last-of-type").each(function (key, value) {
  173. $(value).html(Amouts[key]);
  174. });
  175. // 渲染商品总数量到页面中
  176. $("#totalNum").html(totalNum);
  177. // 渲染商品总金额到页面中
  178. $("#totalPrice").html(totalPrice);
  179. });
  180. // 商品数量
  181. var Counts = [];
  182. var numbers = $("input[type='number']");
  183. Counts = getCounts(numbers);
  184. function getCounts(numbers) {
  185. return numbers.map(function (key, item) {
  186. return parseInt(numbers.get(key).value);
  187. }).get();
  188. };
  189. console.log(Counts);
  190. // 商品单价
  191. var Prices = [];
  192. var Price = $("tbody>tr>td:nth-of-type(5)");
  193. Prices = getPrices(Price);
  194. function getPrices(Price) {
  195. return Price.map(function (key, value) {
  196. return parseInt($(value).html());
  197. }).get();
  198. };
  199. console.log(Prices);
  200. // 计算商品金额
  201. var Amouts = [];
  202. Amouts = getAmouts(Counts, Prices);
  203. function getAmouts(Counts, Prices) {
  204. var amount = [];
  205. for (var i = 0; i < Counts.length; i++) {
  206. am = Counts[i] * Prices[i];
  207. amount.push(am);
  208. };
  209. return amount;
  210. }
  211. console.log(Amouts);
  212. // 把数据渲染到页面中
  213. $("tbody>tr>td:last-of-type").each(function (key, value) {
  214. $(value).html(Amouts[key]);
  215. });
  216. // 获取总数量
  217. var totalNum = 0;
  218. totalNum = num(Counts);
  219. function num(Counts) {
  220. return Counts.reduce(function (prev, next) {
  221. return prev += next;
  222. });
  223. };
  224. console.log(totalNum);
  225. // 渲染到页面中
  226. $("#totalNum").html(totalNum);
  227. // 获取总金额
  228. var totalPrice = 0;
  229. totalPrice = toPrice(Amouts);
  230. function toPrice(Amouts) {
  231. return Amouts.reduce(function (prev, next) {
  232. return prev += next;
  233. });
  234. };
  235. console.log(totalPrice);
  236. // 渲染到页面中
  237. $("#totalPrice").html(totalPrice);
  238. // 绑定事件
  239. numbers.on("change", autoCalculate);
  240. function autoCalculate(ev) {
  241. // 获取商品数量
  242. Counts = getCounts(numbers);
  243. // 获取商品单价
  244. Prices = getPrices(Price);
  245. // 获取商品金额
  246. Amouts = getAmouts(Counts, Prices);
  247. // 获取商品总数量
  248. totalNum = num(Counts);
  249. // 获取商品总金额
  250. totalPrice = toPrice(Amouts);
  251. // 渲染商品金额到页面中
  252. $("tbody>tr>td:last-of-type").each(function (key, value) {
  253. $(value).html(Amouts[key]);
  254. });
  255. // 渲染商品总数量到页面中
  256. $("#totalNum").html(totalNum);
  257. // 渲染商品总金额到页面中
  258. $("#totalPrice").html(totalPrice);
  259. }
  260. </script>
  261. </body>
  262. </html>

总结

1.对于换肤图片案列,明白了一件事一开始一直以为body的高度就是页面的高度,一直想着为什么图片的自适应实现不了,查看控制器才发现body的高度没设置的话是靠其子元素的高度撑开的,不是页面的高度。
2.自动计算案列在获取数量或者单价数组时,用jQuery的map()方法得到的值用parseInt()方法后得到的是jQuery的对象而不是数组,用了get()方法后才是数组,我的想法是parseInt()转为数值,那么应该直接成数组不是吗,这里有点不明白

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:jQuery中, 要注意当前是jQ对象,还是js对象, 这个很重要
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