2_v-on.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #div1{
  10. width: 200px;
  11. height: 200px;
  12. background: red;
  13. }
  14. #div2{
  15. width: 100px;
  16. height: 100px;
  17. background: blue;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="app">
  23. {{msg}}
  24. {{num}}
  25. <!-- <button v-on:click="fn">按钮</button> -->
  26. <!-- v-on:click 简写为 @click-->
  27. <button @click="fn">按钮</button>
  28. <div id="div1" @click="fn1()">
  29. <div id="div2" @click="fn2()"></div>
  30. </div>
  31. <input type="text" @keyup ="fn3()">
  32. <input type="text" @keyup.enter="fn4()">
  33. </div>
  34. <script src="vue.js"></script>
  35. <script>
  36. new Vue({
  37. el:'#app',
  38. data:{
  39. msg:'hello',
  40. num: 20
  41. },
  42. methods:{
  43. fn(){
  44. // console.log(111)
  45. /* 修改data中的值 this. */
  46. this.msg = 'hello word'
  47. this.num++
  48. },
  49. fn1(){
  50. console.log(111)
  51. },
  52. fn2(){
  53. console.log(222)
  54. },
  55. fn3(){
  56. console.log(event.keyCode)
  57. },
  58. fn4(){
  59. console.log('hahhahahah')
  60. }
  61. }
  62. })
  63. </script>
  64. </body>
  65. </html>