5_von.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: pink;
  13. }
  14. .box2{
  15. width: 200px;
  16. height: 200px;
  17. background-color: red;
  18. }
  19. .box3{
  20. width: 100px;
  21. height: 100px;
  22. background-color: blue;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <!-- v-on 为元素绑定事件 v-on:事件名称="方法名" -->
  29. <button v-on:click="fun">按钮</button>
  30. <!-- 如果需要传递参数 可以在方法名后面添加参数 -->
  31. <button v-on:click="fun2('2')">按钮2</button>
  32. <!-- v-on可以缩写为 @ -->
  33. <button @click="fun3">按钮3</button>
  34. <div v-if="isShow"> hello Vue!</div>
  35. <div class="box1" @click="fun4">
  36. <div class="box2" @click="fun5">
  37. <!-- .stop 等同于 stopPropagation 阻止事件冒泡 -->
  38. <div class="box3" @click.stop="fun6"></div>
  39. </div>
  40. </div>
  41. </div>
  42. <script>
  43. new Vue({
  44. el:"#app",
  45. data:{
  46. isShow:true,
  47. },
  48. // methods 内部放置一些自定义的方法 比如事件处理函数
  49. methods:{
  50. fun(){
  51. console.log("hello vue");
  52. },
  53. fun2(i){
  54. console.log(i);
  55. },
  56. fun3(){
  57. console.log("hello vue3");
  58. // 修改data中的值
  59. // vue 中使用this 可以指向data中的值
  60. this.isShow = false;
  61. },
  62. fun4(){
  63. console.log("4");
  64. },
  65. fun5(){
  66. console.log("5");
  67. },
  68. fun6(e){
  69. // e.stopPropagation();
  70. console.log("6");
  71. }
  72. }
  73. })
  74. </script>
  75. </body>
  76. </html>