亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 48
粉絲 0
評(píng)論 0
訪問(wèn)量 44729
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
JS的案列簡(jiǎn)單演示(0821)
丶久而舊之丶
原創(chuàng)
1688人瀏覽過(guò)

案列

使用jQuery實(shí)現(xiàn)功能

一鍵換膚

  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>

全選和自動(dòng)計(jì)算金額

  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>自動(dòng)計(jì)算價(jià)格</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>購(gòu)物車</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>單價(jià)/元</td>
  49. <td>數(shù)量</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>臺(tái)</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手機(jī)</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移動(dòng)硬盤(pán)</td>
  91. <td>個(gè)</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">總計(jì)</td>
  111. <td id="totalNum"></td>
  112. <td id="totalPrice"></td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. <div>
  117. <button>結(jié)算</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. // 獲取商品數(shù)量
  130. Counts = getCounts(numbers);
  131. // 獲取商品單價(jià)
  132. Prices = getPrices(Price);
  133. // 獲取商品金額
  134. Amouts = getAmouts(Counts, Prices);
  135. // 獲取商品總數(shù)量
  136. totalNum = num(Counts);
  137. // 獲取商品總金額
  138. totalPrice = toPrice(Amouts);
  139. // 渲染商品金額到頁(yè)面中
  140. $("tbody>tr>td:last-of-type").each(function (key, value) {
  141. $(value).html(Amouts[key]);
  142. });
  143. // 渲染商品總數(shù)量到頁(yè)面中
  144. $("#totalNum").html(totalNum);
  145. // 渲染商品總金額到頁(yè)面中
  146. $("#totalPrice").html(totalPrice);
  147. }
  148. $("input[name='itemID']").change(function (ev) {
  149. var count = 0;
  150. // 循環(huán)按鈕選中狀態(tài)的個(gè)數(shù)
  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. // 根據(jù)個(gè)數(shù)判斷全選按鈕是否要選ziyu
  160. $("#itemAll").prop("checked", (count === $("input[name='itemID']").length));
  161. // 獲取商品數(shù)量
  162. Counts = getCounts(numbers);
  163. // 獲取商品單價(jià)
  164. Prices = getPrices(Price);
  165. // 獲取商品金額
  166. Amouts = getAmouts(Counts, Prices);
  167. // 獲取商品總數(shù)量
  168. totalNum = num(Counts);
  169. // 獲取商品總金額
  170. totalPrice = toPrice(Amouts);
  171. // 渲染商品金額到頁(yè)面中
  172. $("tbody>tr>td:last-of-type").each(function (key, value) {
  173. $(value).html(Amouts[key]);
  174. });
  175. // 渲染商品總數(shù)量到頁(yè)面中
  176. $("#totalNum").html(totalNum);
  177. // 渲染商品總金額到頁(yè)面中
  178. $("#totalPrice").html(totalPrice);
  179. });
  180. // 商品數(shù)量
  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. // 商品單價(jià)
  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. // 計(jì)算商品金額
  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. // 把數(shù)據(jù)渲染到頁(yè)面中
  213. $("tbody>tr>td:last-of-type").each(function (key, value) {
  214. $(value).html(Amouts[key]);
  215. });
  216. // 獲取總數(shù)量
  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. // 渲染到頁(yè)面中
  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. // 渲染到頁(yè)面中
  237. $("#totalPrice").html(totalPrice);
  238. // 綁定事件
  239. numbers.on("change", autoCalculate);
  240. function autoCalculate(ev) {
  241. // 獲取商品數(shù)量
  242. Counts = getCounts(numbers);
  243. // 獲取商品單價(jià)
  244. Prices = getPrices(Price);
  245. // 獲取商品金額
  246. Amouts = getAmouts(Counts, Prices);
  247. // 獲取商品總數(shù)量
  248. totalNum = num(Counts);
  249. // 獲取商品總金額
  250. totalPrice = toPrice(Amouts);
  251. // 渲染商品金額到頁(yè)面中
  252. $("tbody>tr>td:last-of-type").each(function (key, value) {
  253. $(value).html(Amouts[key]);
  254. });
  255. // 渲染商品總數(shù)量到頁(yè)面中
  256. $("#totalNum").html(totalNum);
  257. // 渲染商品總金額到頁(yè)面中
  258. $("#totalPrice").html(totalPrice);
  259. }
  260. </script>
  261. </body>
  262. </html>

總結(jié)

1.對(duì)于換膚圖片案列,明白了一件事一開(kāi)始一直以為body的高度就是頁(yè)面的高度,一直想著為什么圖片的自適應(yīng)實(shí)現(xiàn)不了,查看控制器才發(fā)現(xiàn)body的高度沒(méi)設(shè)置的話是靠其子元素的高度撐開(kāi)的,不是頁(yè)面的高度。
2.自動(dòng)計(jì)算案列在獲取數(shù)量或者單價(jià)數(shù)組時(shí),用jQuery的map()方法得到的值用parseInt()方法后得到的是jQuery的對(duì)象而不是數(shù)組,用了get()方法后才是數(shù)組,我的想法是parseInt()轉(zhuǎn)為數(shù)值,那么應(yīng)該直接成數(shù)組不是嗎,這里有點(diǎn)不明白

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語(yǔ):jQuery中, 要注意當(dāng)前是jQ對(duì)象,還是js對(duì)象, 這個(gè)很重要
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)