定制设计jQuery+css+html淘宝购物车模块

注意给checkbox定制设计定制设计添加事件就是用change()

给button添加事件就是用click()

1、每次点击+号,定制设计根据文本框的值乘以当定制设计前商品的价格就是商品的小计

2、定制设计只能增加本商品的小计,定制设计就是当前商品的小计模块

3、定制设计修改普通元素的内容是text方法

4、定制设计当前商品的价格要把¥定制设计符号去掉再相乘 substr()

5、parents(‘选择器’)定制设计可以返回指定祖先元素4

6、定制设计最后计算的结果如果想要通过toFixed(2)方法

7、定制设计用户也可以直接修改表定制设计单里面的值,定制设计同样要计算小计,用表单change事件

8、定制设计用最新的表单内容的值定制设计乘以单价即可,定制设计但还是当前商品的小计

定制设计计算总计和总额:

思路:定制设计把所有文本框里面的值定制设计相加就是总计数量,总额同理

文本框里面的值不相同,如果想要相加就需要用到each的遍历,声明一个变量,相加即可

点击+号或者-号,都会改变总计和总额,如果用户修改了文本框里面的值也会改变总额,那么都要分别添加到这三个事件中,因此封装一个函数来求总额和总计,以上几个操作调用即可

注意:总计是文本框中的值相加val()。总额是普通元素的内容text()

注意普通元素里面的内容要去掉¥并且转为数字型后才能相加

删除商品模块:

1、把商品remove()删除元素即可

2、三个地方需要删除:1、商品后面的删除按钮2、删除选中的商品3、清理购物车

1、商品后面的删除按钮:一定是删除当前的商品,所以从$(this)出发

2、删除选中的商品:先判断小的复选框按钮是否选中状态,如果选中,则删除对应的商品

可以不用遍历,$('element:checked')可以直接拿出被选中的复选框

选中商品添加背景色

核心:选中的商品添加背景,不选中的商品移除背景即可

1、全选按钮点击:如果全选是选中的,则所有的商品添加背景,否则移除背景

2、小的复选框点击:如果是选中状态,则当前商品添加背景,否则移除背景

3、这个背景,可以通过类进行修改,添加类和移除类

 

  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. <style>
  9. img {
  10. width: 80px;
  11. height: 80px
  12. }
  13. a {
  14. color: black;
  15. text-decoration: none;
  16. }
  17. .mony {
  18. width: 20px;
  19. text-align: center;
  20. }
  21. </style>
  22. <script src="jquery.min.js"></script>
  23. </head>
  24. <body>
  25. <div>全部商品</div>
  26. <!-- 一个表单元素 -->
  27. <form action="">
  28. <table style="border-collapse: 0px;border-spacing: 0px;width:872px">
  29. <tr style="background-color:grey;">
  30. <td style='width:60px'><input type="checkbox" class="choseall">全选</td>
  31. <td style="width:472px;height:40px">商品</td>
  32. <td style='width:60px'>单价</td>
  33. <td style="width:100px">数量</td>
  34. <td style="width:110px">小计</td>
  35. <td style='width:70px'>操作</td>
  36. </tr>
  37. <tr>
  38. <td><input type="checkbox" class="goodchose"></td>
  39. <td>
  40. <div>
  41. <div style="float:left">
  42. <a href="">
  43. <img src="https://img.alicdn.com/bao/uploaded/i2/2904754564/O1CN011Q0OER1jaMJXxFeHl_!!2904754564.jpg_240x240.jpg" alt="">
  44. </a>
  45. </div>
  46. <div>
  47. <a href="javascript:;">【新势力周】YUKI小树家可爱趣味印花短款白T恤+卡扣高腰宽松复古阔腿牛仔长裤</a>
  48. </div>
  49. </div>
  50. </td>
  51. <td class="onemony">¥37.00</td>
  52. <td>
  53. <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
  54. </td>
  55. <td class="totalmony">¥37.00</td>
  56. <td class="deletthis">删除</td>
  57. </tr>
  58. <tr>
  59. <td><input type="checkbox" class="goodchose"></td>
  60. <td>
  61. <div>
  62. <div style="float:left">
  63. <a href="">
  64. <img src="https://img.alicdn.com/bao/uploaded/i1/3606826698/O1CN01NPCasY1zLjTBGmAax_!!3606826698.jpg_240x240.jpg" alt="">
  65. </a>
  66. </div>
  67. <div>
  68. <a href="javascript:;">日系正肩短袖t恤女夏薄款设计感小众日系甜酷学生小个子宽松上衣</a>
  69. </div>
  70. </div>
  71. </td>
  72. <td class="onemony">¥49.90</td>
  73. <td>
  74. <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
  75. </td>
  76. <td class="totalmony">¥49.90</td>
  77. <td class="deletthis">删除</td>
  78. </tr>
  79. <tr>
  80. <td><input type="checkbox" class="goodchose"></td>
  81. <td>
  82. <div>
  83. <div style="float:left">
  84. <a href="">
  85. <img src="https://img.alicdn.com/bao/uploaded/i3/3606826698/O1CN01jfI3hB1zLjZhqP5rC_!!3606826698.jpg_240x240.jpg" alt="">
  86. </a>
  87. </div>
  88. <div>
  89. <a href="javascript:;">春秋格子衬衫外套女2022新款小个子设计感小众宽松慵懒风日系上衣</a>
  90. </div>
  91. </div>
  92. </td>
  93. <td class="onemony">¥69.90</td>
  94. <td>
  95. <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
  96. </td>
  97. <td class="totalmony">¥69.90</td>
  98. <td class="deletthis">删除</td>
  99. </tr>
  100. <tr>
  101. <td><input type="checkbox" class="goodchose"></td>
  102. <td>
  103. <div>
  104. <div style="float:left">
  105. <a href="">
  106. <img src="https://img.alicdn.com/bao/uploaded/i2/628189716/O1CN01mpRMR22Ldyv4OgUtt_!!0-item_pic.jpg_80x80.jpg" alt="">
  107. </a>
  108. </div>
  109. <div>
  110. <a href="javascript:;">满减【百草味-红油面皮134g】方便面盒装速食凉皮拌泡面米粉凉皮</a>
  111. </div>
  112. </div>
  113. </td>
  114. <td class="onemony">¥23.70</td>
  115. <td>
  116. <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
  117. </td>
  118. <td class="totalmony">¥23.70</td>
  119. <td class="deletthis">删除</td>
  120. </tr>
  121. </table>
  122. <div style="position:fixed;bottom:0px;width:872px;background-color:rgb(184, 177, 177);height:50px;line-height: 50px;">
  123. <input type="checkbox" class="chosealllast">全选<span style="position:absolute;left:70px" class="chosedelet">删除选中的商品</span><span style="position:absolute;left:200px" class="clearall">清理购物车</span>
  124. <span style="position:absolute;right:230px;" class="totoalgoodn">已经选0件商品</span><span style="position:absolute;right:150px;">总价:</span>
  125. <span style="position:absolute;left:715px;color:red;font-weight: bold;" class="totalprice">¥12.60</span>
  126. </div>
  127. </form>
  128. <script>
  129. $(function() {
  130. //1、全选按钮。上下的全选按钮效果是一样的,按钮状态选中,那么所有商品的状态为选中。点击全选按钮让其不选中时,商品的选中状态就全部取消choseall与goodchose
  131. $('.choseall').change(function() {
  132. //让下面商品的选中状态与全选按钮的一致$('.choseall').prop(checked)
  133. $('.goodchose').prop('checked', $('.choseall').prop('checked'))
  134. $('.chosealllast').prop('checked', $('.choseall').prop('checked'))
  135. // //全选按钮按下,那么商品总数就等于goodchose的数量
  136. // if ($('.choseall').prop('checked') == true) {
  137. // $('.totoalgoodn').text('已经选' + $('.goodchose').length + '件商品')
  138. // }
  139. // //取消全选数目就等于0
  140. // if ($('.choseall').prop('checked') == false) {
  141. // $('.totoalgoodn').text('已经选' + 0 + '件商品')
  142. // }
  143. //复制一份到下面的全选按钮中
  144. if ($(this).prop('checked') == true) {
  145. $('.goodchose').parent().parent().css('background-color', 'aliceblue')
  146. } else {
  147. $('.goodchose').parent().parent().css('background-color', '')
  148. }
  149. })
  150. $('.chosealllast').change(function() {
  151. //让下面商品的选中状态与全选按钮的一致$('.choseall').prop(checked)
  152. $('.goodchose').prop('checked', $('.chosealllast').prop('checked'))
  153. $('.choseall').prop('checked', $('.chosealllast').prop('checked'))
  154. // if ($('.choseall').prop('checked') == true) {
  155. // $('.totoalgoodn').text('已经选' + $('.goodchose').length + '件商品')
  156. // }
  157. // //取消全选数目就等于0
  158. // if ($('.choseall').prop('checked') == false) {
  159. // $('.totoalgoodn').text('已经选' + 0 + '件商品')
  160. // }
  161. if ($(this).prop('checked') == true) {
  162. $('.goodchose').parent().parent().css('background-color', 'aliceblue')
  163. } else {
  164. $('.goodchose').parent().parent().css('background-color', '')
  165. }
  166. })
  167. //2、当商品的选中个数小于商品栏个数的时候,两个全选按钮就会取消选中element:checked 用来专门获取复选框被选中的情况.length获取个数
  168. $('.goodchose').change(function() {
  169. // console.log($('.goodchose:checked'))
  170. if ($('.goodchose:checked').length != $('.goodchose').length) {
  171. $('.choseall').prop('checked', false)
  172. $('.chosealllast').prop('checked', false)
  173. }
  174. //当商品的选中个数等于商品个数的时候,就相当于全选了,此时两个全选复选框全部选中
  175. if ($('.goodchose:checked').length == $('.goodchose').length) {
  176. $('.choseall').prop('checked', true)
  177. $('.chosealllast').prop('checked', true)
  178. }
  179. // //6、更改商品总数内容
  180. // $('.totoalgoodn').text('已经选' + $('.goodchose:checked').length + '件商品')
  181. // console.log($('.goodchose:checked').length);
  182. //11
  183. if ($(this).prop('checked') == true) {
  184. $(this).parent().parent().css('background-color', 'aliceblue')
  185. } else {
  186. $(this).parent().parent().css('background-color', '')
  187. }
  188. })
  189. //3、修改数量为jian和add添加事件
  190. $('.add').click(function() {
  191. //这个商品的价格改变,siblings('')
  192. //是在初始值上每点击一次就减一,但最小为一
  193. var b = $(this).siblings('.mony').prop('value')
  194. //这个b是一个字符型
  195. $(this).siblings('.mony').prop('value', parseInt(b) + 1)
  196. //4|
  197. var c = $(this).parent().siblings('.onemony').text().substr(1)
  198. // console.log(c)
  199. //更改小计里面的内容,显示两个小数.toFixed(2)
  200. $(this).parent().siblings('.totalmony').text('¥' + (c * (parseInt(b) + 1)).toFixed(2))
  201. totalnumber()
  202. })
  203. $('.jian').click(function() {
  204. //这个商品的价格改变,siblings('')
  205. //是在初始值上每点击一次就减一,但最小为一
  206. var a = parseInt($(this).siblings('.mony').prop('value'))
  207. if (a >= 2) {
  208. $(this).siblings('.mony').prop('value', a - 1)
  209. //4|
  210. var c = $(this).parent().siblings('.onemony').text().substr(1)
  211. // console.log(c)
  212. //更改小计里面的内容,显示两个小数.toFixed(2)
  213. $(this).parent().siblings('.totalmony').text('¥' + (c * (a - 1)).toFixed(2))
  214. }
  215. totalnumber()
  216. })
  217. //4、实现小计跟随数量变化,用数量的值*单价。单价去掉¥,得到数字,因为是跟随数量变化的,所以这一部分添加到点击事件中去,substr()
  218. //5、手动更改数量的时候,小计会发生改变.给mony添加change事件
  219. $('.mony').change(function() {
  220. //获得这里面的值赋值给一个变量
  221. var number = $(this).prop('value')
  222. //获取对应的单价
  223. var c = $(this).parent().siblings('.onemony').text().substr(1)
  224. //要保证合格值大于等于1
  225. if (number >= 1) {
  226. var b = number * c
  227. }
  228. //当number小于1时,默认为1
  229. if (number < 1) {
  230. $(this).prop('value', 1)
  231. number = 1
  232. var b = number * c
  233. }
  234. // alert(number)
  235. // console.log(number)
  236. console.log(b)
  237. $(this).parent().siblings('.totalmony').text('¥' + (b).toFixed(2))
  238. totalnumber()
  239. })
  240. //6、获得已经选择的商品个数,就是看商品栏的状态选择个数,再状态变化的事件中进行更改--不对,商品数目拿的是表单里面的数量综合,一旦表单里面的元素值发生改变的时候,就要重新计算以下商品的数目
  241. //7、更改价格,价格的话就是小计的累加。那么就需要对小计进行遍历,一旦小计发生改变,就需要遍历一次去改变总价
  242. //函数封装
  243. //总计
  244. function totalnumber() {
  245. var ton = 0
  246. $('.mony').each(function(index, ele) {
  247. // console.log(index);
  248. // console.log($('.mony').eq(index).val());直接拿ele这个元素里面的内容,不需要进行索引
  249. ton = ton + parseInt($(ele).val())
  250. })
  251. $('.totoalgoodn').text('已经选' + ton + '件商品')
  252. //总额,从小计里面获得,去掉¥,再遍历相加
  253. var mon = 0
  254. $('.totalmony').each(function(i, e) {
  255. //首先获取每个元素的text
  256. mon = mon + parseInt($(e).text().substr(1))
  257. })
  258. //把mon给总价后的数值
  259. $('.totalprice').text('¥' + mon.toFixed(2))
  260. }
  261. totalnumber() //打开这个页面,有默认的值存在,所以也要调用一次
  262. //8、按下删除键,就删除此时的商品
  263. $('.deletthis').click(function() {
  264. //删除我的父亲tr
  265. $(this).parent().remove()
  266. totalnumber()
  267. })
  268. //9、删除选中的商品
  269. $('.chosedelet').click(function() {
  270. //要获取我选中的商品是那些,并将其删除
  271. //对所有的goodchose进行遍历
  272. // $('.goodchose').each(function(index, ele) {
  273. // //判断如果当前元素ele的状态是true,则将该元素的父亲的父亲tr删除
  274. // if ($(ele).prop('checked') == true) {
  275. // $(ele).parent().parent().remove()
  276. // }
  277. // })
  278. //可以不用遍历
  279. $('.goodchose:checked').parent().parent().remove()
  280. totalnumber()
  281. })
  282. //10、按下清理购物车,就将所有的商品都清理掉
  283. $('.clearall').click(function() {
  284. //这里不需要进行遍历
  285. $('.goodchose').parent().parent().remove()
  286. totalnumber()
  287. })
  288. //11、选定一个商品就给其添加一个背景色,放到商品购物状态change事件中
  289. })
  290. </script>
  291. </body>
  292. </html>

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发