14.事件绑定.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: 400px;
  10. height: 400px;
  11. background: #00f;
  12. }
  13. #box1 {
  14. width: 200px;
  15. height: 200px;
  16. background: #ff0;
  17. }
  18. .news {
  19. width: 200px;
  20. height: 200px;
  21. padding: 20px;
  22. border: 1px solid #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <!--
  29. v-on 事件绑定 可以简写成 @
  30. 格式:
  31. @事件名="事件方法"
  32. v-on:事件名="事件方法"
  33. 修饰符
  34. stop 阻止事件冒泡
  35. prevent 阻止默认事件
  36. once 只触发一次
  37. self 仅自身才能触发
  38. native 组件无法绑定原生时间 可以使用native绑定
  39. @keydown @keyup
  40. -->
  41. <h1>{{msg}}</h1>
  42. <input type="text" @keydown="keyThing">
  43. <!-- <div v-on:click="getPart1">事件发生</div> -->
  44. <div @click="getPart1">事件发生</div>
  45. <a href="https://www.baidu.com" @click.prevent.once="showHi">你好</a>
  46. <div id="box" @click="getPart2">
  47. <div id="box1" @click.stop="getPart3"></div>
  48. </div>
  49. <button @click.once="pay">一次性支付</button>
  50. <div class="news" @click.self="newThing">
  51. <span @click="newHi" style="width: 100px;height: 100px;background: #000;color: #fff;">你好啊</span>
  52. </div>
  53. </div>
  54. <script src="./vue.js"></script>
  55. <script>
  56. var app = new Vue({
  57. el:"#app",
  58. data:{
  59. msg:"你好啊"
  60. },
  61. methods:{
  62. keyThing(event) {
  63. console.log(event,'键盘')
  64. },
  65. getPart1() {
  66. console.log("触发一")
  67. },
  68. getPart2() {
  69. console.log("触发2")
  70. },
  71. getPart3(event){
  72. // event.stopPropagation();
  73. console.log("触发3")
  74. },
  75. showHi(event) {
  76. // event.preventDefault();
  77. console.log("你好",event)
  78. },
  79. pay() {
  80. console.log("支付")
  81. },
  82. newThing() {
  83. console.log("哈哈哈哈")
  84. },
  85. newHi() {
  86. console.log("你好")
  87. }
  88. }
  89. })
  90. </script>
  91. </body>
  92. </html>