码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 小小购物车案例(V3)


    效果如下:

    可以添加和减少商品个数(最少个为1),在添加的时候总价格会随着改变,也可以点击按钮移除商品

    代码分为三个模块(html、js、css)

    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. <link href="./index.css" rel="stylesheet" type="text/css">
    8. </head>
    9. <body>
    10. <div id="app"></div>
    11. <template id="my-app">
    12. <template v-if="books.length > 0">
    13. <table>
    14. <thead>
    15. <th>序号</th>
    16. <th>书集名称</th>
    17. <th>出版日期</th>
    18. <th>价格</th>
    19. <th>购买数量</th>
    20. <th>操作</th>
    21. </thead>
    22. <tbody>
    23. <tr v-for="(book,index) in books" :key="index">
    24. <td>{{index+1}}</td>
    25. <td>{{book.name}}</td>
    26. <td>{{book.publicationDate}}</td>
    27. <td>{{formatPrice(book.price)}}</td>
    28. <td>
    29. <button :disabled="book.count<=1" @click="decrement(index)">-</button>
    30. <span class="count">{{book.count}}</span>
    31. <button @click="increment(index)">+</button>
    32. </td>
    33. <td>
    34. <button @click="removeBook(index)">移除</button>
    35. </td>
    36. </tr>
    37. </tbody>
    38. </table>
    39. <h2>总价格:{{formatPrice(totalPrice)}}</h2>
    40. </template>
    41. <template v-else>
    42. <div class="empty">购物车为空</div>
    43. </template>
    44. </template>
    45. <script src="../js/vue3.js"></script>
    46. <script src="./index.js"></script>
    47. </body>
    48. </html>

    js部分:

    1. const App = {
    2. template: '#my-app',
    3. data() {
    4. return {
    5. books: [
    6. {
    7. id: 1,
    8. name: '乱世佳人',
    9. publicationDate: '2007-09-02',
    10. price: '42',
    11. count: 1,
    12. },
    13. {
    14. id: 2,
    15. name: '幸福来敲门',
    16. publicationDate: '2007-09-02',
    17. price: '54',
    18. count: 1,
    19. },
    20. {
    21. id: 3,
    22. name: '弱点',
    23. publicationDate: '2007-09-02',
    24. price: '65',
    25. count: 1,
    26. },
    27. {
    28. id: 4,
    29. name: '楚门的世界',
    30. publicationDate: '2007-09-02',
    31. price: '43',
    32. count: 1,
    33. }
    34. ]
    35. }
    36. },
    37. computed: {
    38. totalPrice() {
    39. let finalPrice = 0;
    40. for (let book of this.books) {
    41. finalPrice += book.count * book.price;
    42. }
    43. return finalPrice
    44. }
    45. },
    46. methods: {
    47. // 点击加
    48. increment(index) {
    49. this.books[index].count++;
    50. },
    51. // 点击减
    52. decrement(index) {
    53. this.books[index].count--;
    54. },
    55. // 点击移除
    56. removeBook(index) {
    57. this.books.splice(index, 1)
    58. },
    59. // 在价格前面加上¥符号
    60. formatPrice(price) {
    61. return "¥" + price
    62. }
    63. },
    64. }
    65. Vue.createApp(App).mount('#app')

    css部分:

    1. table{
    2. border: 1px solid #e9e9e9;
    3. border-collapse: collapse;
    4. border-spacing: 0;
    5. }
    6. th,td{
    7. padding: 8px 16px;
    8. border: 1px solid #e9e9e9;
    9. }
    10. th{
    11. background-color: #f7f7f7;
    12. color: #5c6b77;
    13. font-weight: 600;
    14. }
    15. .count{
    16. margin: 0 5px;
    17. }
    18. .empty{
    19. margin-left: 80px;
    20. margin-top: 50px;
    21. font-size: 30px;
    22. }

  • 相关阅读:
    SpringBoot请求参数与响应返回值,ResponseEntity<T>自定义响应
    【附源码】Python计算机毕业设计宁夏源沣医药线上销售平台
    网络规划设计师之OSI七层模型之应用层
    【Web漏洞探索】跨站脚本漏洞
    3D漫游:所见即所得的形式,构建线上数字展厅
    动手学习深度学习 06:卷积神经网络
    IIS 日志解析,增强 IIS 服务器安全性
    【毕业设计】基于javaEE+SSH+SqlServer的企业车辆管理系统设计与实现(毕业论文+程序源码)——车辆管理系统
    Azure Synapse Analytics 性能优化指南(3)——使用具体化视图优化性能(下)
    建模杂谈系列175 论交付与共赢
  • 原文地址:https://blog.csdn.net/weixin_45096939/article/details/133175547
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号