7_v-model.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <div class="box">{{str}}</div>
  11. <!-- v-model 指令:实现表单元素和数据的双向绑定 -->
  12. <!--v-model.trim 指令:自动去除首尾空格 -->
  13. <!-- <input type="text" v-model.trim="str"> -->
  14. <!-- v-model.number 指令:将输入值转换为数字类型 -->
  15. <!-- <input type="text" v-model.number="str"> -->
  16. <!-- v-model.lazy 指令:在输入框失去焦点时更新数据 -->
  17. <!-- input事件:当输入框的值发生变化时触发 -->
  18. <!-- <input @input="inputFun" type="text" v-model.lazy="str"> -->
  19. <!-- change事件:当输入框失去焦点时触发 -->
  20. <input @change="inputFun" type="text" v-model.lazy="str">
  21. <button @click="changeStr">提交</button>
  22. </div>
  23. <script src="./js/vue.js"></script>
  24. <script>
  25. let a = 10;
  26. new Vue({
  27. el: "#app",
  28. data: {
  29. str: "",
  30. },
  31. methods: {
  32. changeStr() {
  33. // this.str = 100;
  34. console.log(typeof this.str);
  35. console.log(a);
  36. },
  37. inputFun() {
  38. console.log("input事件触发");
  39. }
  40. }
  41. })
  42. </script>
  43. </body>
  44. </html>