7.vue事件处理.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. .box {
  9. width: 200px;
  10. height: 200px;
  11. background: #00f;
  12. color: #fff;
  13. font-size: 30px;
  14. }
  15. .box1 {
  16. width: 300px;
  17. height: 300px;
  18. background: #f00;
  19. }
  20. .box2 {
  21. width: 150px;
  22. height: 150px;
  23. background: #ff0;
  24. }
  25. .box3 {
  26. width: 300px;
  27. height: 300px;
  28. background: #f00;
  29. }
  30. .box4 {
  31. width: 150px;
  32. height: 150px;
  33. background: #ff0;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="app">
  39. <!--
  40. 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。
  41. v-on 事件处理 =>简写:"@"
  42. 格式:
  43. 1.v-on:事件名="事件处理"
  44. 2.@事件名="事件处理"
  45. 修饰符:
  46. 阻止事件冒泡 .stop
  47. 阻止默认事件 .prevent
  48. 触发一次 .once
  49. 触发自身 .self
  50. -->
  51. <!-- 键盘事件 -->
  52. <input type="text" @keyup.enter="showEnter">
  53. <input type="text" @keydown.tab="showEnter">
  54. <input type="text" @keydown.tot="showEnter">
  55. <br>
  56. <br>
  57. <br>
  58. <div class="box" v-on:click="num++">{{num}}</div>
  59. <div @click="showAlert">
  60. 弹出框
  61. </div>
  62. <div @click="showMsg(1)">
  63. 消息
  64. </div>
  65. <div @click="showEvent($event)">
  66. 事件
  67. </div>
  68. <!-- 事件冒泡 -->
  69. <div class="box1" @click="getMain">
  70. <div class="box2" @click.stop="getMsg"></div>
  71. </div>
  72. <!-- 默认事件 -->
  73. <a href="https://v2.cn.vuejs.org/v2/guide/events.html" @click.prevent="getSee">Vue官网</a>
  74. <!-- 触发一次 -->
  75. <div @click.once="showContent">内容</div>
  76. <!-- 触发自身 -->
  77. <div @click.self="mySelf" class="box3">
  78. <div @click="meMain" class="box4">
  79. 我自己
  80. </div>
  81. </div>
  82. </div>
  83. <script src="./vue.js"></script>
  84. <script>
  85. // 自定义键值
  86. Vue.config.keyCodes.tot = 32;
  87. var app = new Vue({
  88. el:"#app",
  89. data:{
  90. num: 0
  91. },
  92. methods:{
  93. showAlert() {
  94. alert("出现");
  95. },
  96. showMsg(a) {
  97. console.log(a,'a');
  98. },
  99. showEvent(a) {
  100. console.log(a.target,'a1');
  101. },
  102. getMain() {
  103. alert(1);
  104. },
  105. getMsg() {
  106. alert(2);
  107. },
  108. getSee() {
  109. alert(3);
  110. },
  111. showContent() {
  112. alert("555");
  113. },
  114. mySelf() {
  115. alert("666");
  116. },
  117. meMain() {
  118. alert("777");
  119. },
  120. showEnter(event) {
  121. console.log(event.target.value)
  122. }
  123. }
  124. })
  125. </script>
  126. </body>
  127. </html>