5_v-on绑定事件.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. .div1{
  10. width: 200px;
  11. height: 200px;
  12. background-color: blue;
  13. }
  14. .div2{
  15. width: 100px;
  16. height: 100px;
  17. background-color: red;
  18. }
  19. .div3{
  20. width: 100px;
  21. height: 100px;
  22. background-color: green;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <!-- <button v-on:click="clickFun">按钮</button> -->
  29. <!-- <button v-on:click="clickFun('hello')">按钮</button> -->
  30. <div v-on:click="div1Click" class="div1">
  31. <!-- 在vue中获取事件对象使用 $event -->
  32. <!-- <div v-on:click="div2Click($event)" class="div2"></div> -->
  33. <div v-on:click.stop="div2Click($event)" class="div2"></div>
  34. </div>
  35. <!-- <div class="div3" v-on:contextmenu="showMenu($event)"></div> -->
  36. <div class="div3" v-on:contextmenu.prevent="showMenu"></div>
  37. <button @click="click3Fun">简写v-on事件</button>
  38. </div>
  39. <script>
  40. let app = new Vue({
  41. el:"#app",
  42. methods: { //放置方法函数
  43. clickFun:function(val){
  44. console.log(val);
  45. },
  46. div1Click:function(){
  47. console.log("div1被点击了");
  48. },
  49. div2Click:function(e){
  50. // console.log(e);
  51. // // 用事件对象阻止冒泡
  52. // e.stopPropagation();
  53. console.log("div2被点击了");
  54. },
  55. showMenu:function(e){
  56. // 用事件对象阻止默认行为
  57. // e.preventDefault();
  58. console.log("右键菜单")
  59. },
  60. click3Fun:function(){
  61. console.log("v-on简写");
  62. }
  63. },
  64. })
  65. </script>
  66. </body>
  67. </html>