14.事件綁定.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. <style>
  8. .box1 {
  9. width: 200px;
  10. height: 200px;
  11. background: #00f;
  12. }
  13. .box2 {
  14. width: 400px;
  15. height: 400px;
  16. background: #f00;
  17. margin-top: 40px;
  18. }
  19. .box3 {
  20. width: 200px;
  21. height: 200px;
  22. background: #ff0;
  23. }
  24. .box4 {
  25. width: 300px;
  26. height: 300px;
  27. background: pink;
  28. margin-top: 20px;
  29. }
  30. .box5 {
  31. width: 400px;
  32. height: 400px;
  33. background: #0f0;
  34. margin-top: 40px;
  35. }
  36. .box6 {
  37. width: 200px;
  38. height: 200px;
  39. background: #0ff;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div id="app">
  45. <!--
  46. 事件綁定:
  47. v-on 用于事件綁定 可以:@
  48. 格式:
  49. @事件名="事件方法"
  50. v-on:事件名 = "事件方法"
  51. 修飾符:
  52. 阻止默認事件 .prevent
  53. 阻止事件冒泡 .stop
  54. 觸發一次 .once
  55. 觸發自身 .self
  56. .native 穿透
  57. -->
  58. <input type="text" v-on:keydown="showKeyDown">
  59. <input type="text" v-on:keyup="showKeyUp">
  60. <input type="text" v-on:keydown.a="showKeyTab">
  61. <br>
  62. <div class="box1" v-on:click="getBox1"></div>
  63. <!-- <div class="box1" @click="getBox1"></div> -->
  64. <a href="https://www.baidu.com" @click.prevent="getHi">你好</a>
  65. <div class="box2" @click="getBox2">
  66. <div class="box3" @click.stop="getBox3"></div>
  67. </div>
  68. <div class="box4" @click.once='getBox4'></div>
  69. <div @click.self="getBox5" class="box5">
  70. <div @click="getBox6" class="box6"></div>
  71. </div>
  72. </div>
  73. <script src="./vue.js"></script>
  74. <script>
  75. var app = new Vue({
  76. el:"#app",
  77. methods:{
  78. getBox1() {
  79. alert("Box1")
  80. },
  81. getHi() {
  82. alert("你好")
  83. // event.preventDefault()
  84. },
  85. getBox2() {
  86. alert("Box2")
  87. },
  88. getBox3() {
  89. alert("Box3")
  90. // event.stopPropagation()
  91. },
  92. getBox4() {
  93. console.log('box4')
  94. },
  95. getBox5() {
  96. console.log('box5')
  97. },
  98. getBox6() {
  99. console.log('box6')
  100. },
  101. showKeyDown(e) {
  102. console.log("按下",e)
  103. },
  104. showKeyUp() {
  105. console.log("擡起")
  106. },
  107. showKeyTab(e) {
  108. console.log(e)
  109. }
  110. }
  111. })
  112. </script>
  113. </body>
  114. </html>