3_v-on.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. <script src="./js/vue.js"></script>
  8. <style>
  9. .box1{
  10. width: 400px;
  11. height: 400px;
  12. background-color: red;
  13. }
  14. .box2{
  15. width: 200px;
  16. height: 200px;
  17. background-color: blue;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="app">
  23. <!-- v-on 事件绑定 v-on:事件类型=事件处理函数-->
  24. <button v-on:click="btnFun">按钮</button>
  25. <div class="box1" v-on:click="box1Fun">
  26. <!-- v-on 修饰符 .stop阻止事件冒泡 -->
  27. <div class="box2" v-on:click.stop="box2Fun"></div>
  28. </div>
  29. <!-- v-on 修饰符 .prevent阻止默认行为 -->
  30. <div class="box2" v-on:contextmenu.prevent="rightClick"></div>
  31. </div>
  32. <script>
  33. new Vue({
  34. el:"#app",
  35. methods:{
  36. btnFun(){
  37. console.log("hello world");
  38. },
  39. box1Fun(){
  40. console.log("box1")
  41. },
  42. box2Fun(){
  43. console.log("box2")
  44. },
  45. rightClick(){
  46. console.log("右键点击")
  47. }
  48. }
  49. })
  50. </script>
  51. </body>
  52. </html>