经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue实现简易记事本功能
来源:jb51  时间:2021/11/22 13:42:08  对本文有异议

本文实例为大家分享了Vue实现简易记事本功能的具体代码,供大家参考,具体内容如下

预览图:

功能如下:

(1)输入任务并按下回车键,可将任务添加至任务列表(不可输入重复任务)

(2)点击删除,可删除对应任务

(3)点击清空,所有任务都会被删除

(4)左下角同步显示任务总数

完整代码如下:

  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. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #todoapp {
  14. width: 600px;
  15. background-color: rgba(19, 161, 114, 0.63);
  16. font-family: sans-serif;
  17. }
  18. .header>h1 {
  19. padding: 20px 0;
  20. text-align: center;
  21. font-size: 40px;
  22. color: whitesmoke;
  23. }
  24. .newTask {
  25. display: block;
  26. width: 500px;
  27. height: 50px;
  28. line-height: 50px;
  29. padding-left: 10px;
  30. margin: 0 auto;
  31. font-size: 20px;
  32. outline: none;
  33. border: none;
  34. }
  35. .todolist li {
  36. height: 30px;
  37. line-height: 30px;
  38. padding-left: 15px;
  39. margin: 10px 0;
  40. font-size: 25px;
  41. color: white;
  42. }
  43. .todolist .item {
  44. margin-left: 15px;
  45. }
  46. .destroy,
  47. .clear {
  48. width: 50px;
  49. height: 30px;
  50. float: right;
  51. color: white;
  52. background-color: transparent;
  53. border: none;
  54. font-size: 20px;
  55. }
  56. .footer {
  57. width: 600px;
  58. height: 30px;
  59. padding: 10px 0;
  60. vertical-align: middle;
  61. }
  62. .footer p {
  63. display: inline-block;
  64. padding-left: 15px;
  65. color: white;
  66. font-size: 20px;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <section id="todoapp">
  72. <header class="header">
  73. <h1>记事本</h1>
  74. <input type="text" v-model="newItem" class="newTask" placeholder="请输入任务" @keyup.enter="add">
  75. </header>
  76. <section>
  77. <ul class="todolist">
  78. <li v-for="(item, index) in list">
  79. <div>
  80. <span>{{ index + 1 }}</span>
  81. <label class="item">{{ item }}</label>
  82. <button class="destroy" @click="del(index)">删除</button>
  83. </div>
  84. </li>
  85. </ul>
  86. </section>
  87. <footer class="footer">
  88. <p class="count">
  89. items: {{ list.length }}
  90. </p>
  91. <button class="clear" @click="clear" v-show="list.length != 0">清空</button>
  92. </footer>
  93. </section>
  94. <script src="./vue.js"></script>
  95. <script>
  96. const app = new Vue({
  97. el: "#todoapp",
  98. data: {
  99. list: [],
  100. newItem: ""
  101. },
  102. methods: {
  103. add() {
  104. if (this.newItem == "") {
  105. return;
  106. }
  107. else {
  108. if (!this.list.includes(this.newItem)) {
  109. this.list.push(this.newItem);
  110. this.newItem = "";
  111. }
  112. else {
  113. alert("请勿添加重复事件!");
  114. this.newItem = "";
  115. }
  116. }
  117. },
  118. del(index) {
  119. this.list.splice(index, 1);
  120. },
  121. clear() {
  122. this.list = [];
  123. }
  124. }
  125. })
  126. </script>
  127. </body>
  128. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号