博客列表 >条件渲染与购物车

条件渲染与购物车

P粉036614676
P粉036614676原创
2022年09月05日 14:40:37332浏览

1.条件渲染

  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>条件渲染</title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. <style>
  10. .active{
  11. display: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- 原生: <p>今天是星期天</p>
  17. <button onclick="change()">显示</button>
  18. <script>
  19. function change()
  20. {
  21. let p = document.querySelector('p');
  22. let button = document.querySelector('button');
  23. if(button.textContent == '显示')
  24. {
  25. button.textContent = '隐藏';
  26. p.className = 'active';
  27. }
  28. else
  29. {
  30. button.textContent = '显示';
  31. p.classList.remove('active');
  32. }
  33. }
  34. </script> -->
  35. <!-- Vue: <div class="app">
  36. <p v-if="flag">今天有个好天气</p>
  37. <button @click = 'flag = !flag' v-text="flag?'显示':'隐藏'"></button>
  38. </div>
  39. <script>
  40. Vue.createApp({
  41. data(){
  42. return{
  43. flag:true,
  44. }
  45. }
  46. }).mount('.app');
  47. </script>
  48. -->
  49. <div class="app">
  50. <p v-if="point>=1000 && point< 2000">{{grade[0]}}</p>
  51. <p v-else-if="point>=2000 && point< 3000">{{grade[1]}}</p>
  52. <p v-else-if="point>=3000 && point< 4000">{{grade[2]}}</p>
  53. <p v-if="point>=4000">{{grade[3]}}</p>
  54. </div>
  55. <script>
  56. Vue.createApp({
  57. data(){
  58. return{
  59. grade: ['纸片会员', '木头会员', '铁皮会员', '金牌会员', '非会员'],
  60. // 积分
  61. point: 1500,
  62. }
  63. }
  64. }).mount('.app');
  65. </script>
  66. </body>
  67. </html>

购物车

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>计算属性,侦听器属性</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. <style>
  10. table {
  11. width: 20em;
  12. height: 10em;
  13. text-align: center;
  14. border-collapse: collapse;
  15. margin: 1em;
  16. }
  17. table caption {
  18. font-size: 1.2em;
  19. padding: 1em;
  20. margin-bottom: 2em;
  21. border-bottom: 1px solid;
  22. }
  23. tbody tr th:first-of-type {
  24. background: linear-gradient(to left, lightcyan, #fff);
  25. border-right: 1px solid;
  26. }
  27. body tbody tr:not(:last-of-type) > * {
  28. border-bottom: 1px solid;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="app">
  34. <table>
  35. <caption>
  36. 商品结算
  37. </caption>
  38. <tbody>
  39. <tr>
  40. <th>ID</th>
  41. <td>HA110</td>
  42. </tr>
  43. <tr>
  44. <th>品名</th>
  45. <td>伊利纯牛奶</td>
  46. </tr>
  47. <tr>
  48. <th>单价</th>
  49. <td>100</td>
  50. </tr>
  51. <tr>
  52. <th>数量</th>
  53. <!-- v-model:input中输入多少时num就变成多少 -->
  54. <td><input type="number" v-model="num" style="width: 5em" /></td>
  55. </tr>
  56. <tr>
  57. <th>金额</th>
  58. <!-- <td>{{num * price}}</td> -->
  59. <td>{{amount}}</td>
  60. </tr>
  61. </tbody>
  62. </table>
  63. <p style="font-size: 1.2em">
  64. 实付金额: {{realAmount}}, 优惠了 :
  65. <span style="color: red">{{difAmount}}</span>
  66. </p>
  67. </div>
  68. <script>
  69. const app = Vue.createApp({
  70. data() {
  71. return {
  72. // 单价
  73. price: 100,
  74. // 数量
  75. num: 0,
  76. // 折扣
  77. discount: 1,
  78. };
  79. },
  80. // 计算属性(访问器属性)
  81. computed: {
  82. // 计算属性金额 = 单价 * 数量
  83. // get amount(){
  84. // }
  85. amount: {
  86. get() {
  87. return this.price * this.num;
  88. },
  89. set(value) {
  90. //...
  91. },
  92. },
  93. },
  94. // 侦听器属性
  95. watch: {
  96. // 访问器属性
  97. // 被侦听的属性,其实是一个方法,它有二个参数
  98. // 第一个参数是新值(当前值),第二个参数是原值(旧值)
  99. amount(current, origin) {
  100. console.log(current, origin);
  101. // 根据当前金额确定打折
  102. switch (true) {
  103. // 1000-2000: 9折
  104. case current >= 1000 && current < 2000:
  105. this.discount = 0.9;
  106. break;
  107. // 2000 -> 3000 : 8折
  108. case current >= 2000 && current < 3000:
  109. this.discount = 0.8;
  110. break;
  111. // 3000 -> 4000 : 7折
  112. case current >= 3000 && current < 4000:
  113. this.discount = 0.7;
  114. break;
  115. // 4000 -> 5000 : 6折
  116. case current >= 4000 && current < 5000:
  117. this.discount = 0.6;
  118. break;
  119. // 5000 : 5折
  120. case current >= 5000:
  121. this.discount = 0.5;
  122. }
  123. // 实付金额 = 原始金额 * 折扣率
  124. this.realAmount = this.amount * this.discount;
  125. // 优惠金额(差价) = 原始金额 - 实付金额
  126. this.difAmount = this.amount - this.realAmount;
  127. },
  128. },
  129. // 实例生命周期: 当实例与挂载点绑定成功时,自动执行
  130. mounted() {
  131. //初始化商品数量,默认为1
  132. this.num = 1;
  133. },
  134. }).mount('.app');
  135. </script>
  136. </body>
  137. </html>

原生
```html
<!DOCTYPE html>

<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>计算属性,侦听器属性</title>
<style>
table {
width: 20em;
height: 10em;
text-align: center;
border-collapse: collapse;
margin: 1em;
}

  1. table caption {
  2. font-size: 1.2em;
  3. padding: 1em;
  4. margin-bottom: 2em;
  5. border-bottom: 1px solid;
  6. }
  7. tbody tr th:first-of-type {
  8. background: linear-gradient(to left, lightcyan, #fff);
  9. border-right: 1px solid;
  10. }
  11. body tbody tr:not(:last-of-type) > * {
  12. border-bottom: 1px solid;
  13. }
  14. </style>

</head>
<body>
<div class="app">
<table>
<caption>
商品结算
</caption>
<tbody>
<tr>
<th>ID</th>
<td>HA110</td>
</tr>
<tr>
<th>品名</th>
<td>伊利纯牛奶</td>
</tr>
<tr>
<th>单价</th>
<td>100</td>
</tr>
<tr>
<th>数量</th>

  1. <td><input type="number" onchange="change()" style="width: 5em" /></td>
  2. </tr>
  3. <tr>
  4. <th>金额</th>
  5. <td></td>
  6. </tr>
  7. </tbody>
  8. </table>
  9. <p style="font-size: 1.2em">
  10. 实付金额:
  11. <p class="money"></p>
  12. <span style="color: red" class="sp"></span>
  13. </p>
  14. </div>
  15. <script>
  16. function change()
  17. {
  18. let input = document.querySelector('td input');
  19. if(input.value < 0)
  20. {
  21. // let p = window.prompt('从新输入');
  22. // console.log(p);
  23. window.prompt('从新输入');
  24. input.value = 0;
  25. }
  26. let p = document.querySelector('.money');
  27. let span = document.querySelector('.sp');
  28. p.textContent = 100 * input.value;
  29. if( p.textContent > 800)
  30. {
  31. p.textContent = p.textContent * 0.8;
  32. span.textContent ='优惠了:' + p.textContent * 0.2;
  33. // console.log(span);
  34. }
  35. }
  36. </script>

</body>
</html>

```

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议