2.事件修饰符.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>事件修饰符</title>
  8. <!-- 引入vue -->
  9. <script type="text/javascript" src="../js/vue.js"></script>
  10. <style>
  11. * {
  12. margin-top: 20px;
  13. }
  14. .demo1 {
  15. height: 50px;
  16. background-color: skyblue;
  17. }
  18. .box1 {
  19. padding: 5px;
  20. background-color: skyblue;
  21. }
  22. .box2 {
  23. padding: 5px;
  24. background-color: orange;
  25. }
  26. .list{
  27. width: 200px;
  28. height: 200px;
  29. background-color: peru;
  30. overflow: auto;
  31. }
  32. li{
  33. height: 100px;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <!--
  39. Vue中的事件修饰符:
  40. 1、prevent:阻止默认事件(常用);
  41. 2、stop:阻止事件冒泡(常用);
  42. 3、once:事件只触发一次(常用);
  43. 4、capture:使用事件的捕获模式;
  44. 5、self:只有event.target是当前操作的元素时才触发事件;
  45. 6、passive:事件的默认行为立即执行,无需等待事件回调执行完毕;(开发移动端时使用较多)
  46. -->
  47. <!--准备好一个容器-->
  48. <div id="root">
  49. <h2>欢迎来到{{name}}学习</h2>
  50. <!-- prevent:阻止默认事件(常用) -->
  51. <a href="http://www.atguigu.com" @click.prevent="showInfo">点我提示信息</a>
  52. <!--stop:阻止事件冒泡(常用)-->
  53. <div class="demo1" @click="showInfo">
  54. <!-- <button @click.stop="showInfo">点我提示信息</button> -->
  55. <!-- 修饰符可以连续写 -->
  56. <a href="http://www.atguigu.com" @click.stop.prevent="showInfo">点我提示信息</a>
  57. </div>
  58. <!-- once:事件只触发一次(常用) -->
  59. <button @click.once="showInfo">点我提示信息</button>
  60. <!-- capture:使用事件的捕获模式 -->
  61. <div class="box1" @click.capture="showMsg(1)">
  62. div1
  63. <div class="box2" @click="showMsg(2)">
  64. div2
  65. </div>
  66. </div>
  67. <!-- self:只有event.target是当前操作的元素时才触发事件 -->
  68. <div class="demo1" @click.self="showInfoSelf">
  69. <button @click="showInfoSelf">点我提示信息</button>
  70. </div>
  71. <!-- passive:事件的默认行为立即执行,无需等待事件回调执行完毕 -->
  72. <!-- scroll:滚动条发生滚动触发的事件 -->
  73. <!-- wheel:鼠标滚轮发生滚动触发的事件 -->
  74. <ul @wheel.passive="demo" class="list">
  75. <li>1</li>
  76. <li>2</li>
  77. <li>3</li>
  78. <li>4</li>
  79. </ul>
  80. </div>
  81. </body>
  82. <script type="text/javascript">
  83. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示
  84. const vm = new Vue({
  85. el: '#root',
  86. data: {
  87. name: '尚硅谷'
  88. },
  89. methods: {
  90. showInfo(e) {
  91. // e.preventDefault() //阻止默认事件
  92. // e.stopPropagation() //阻止事件冒泡
  93. alert('同学你好!')
  94. },
  95. showMsg(msg) {
  96. console.log(msg);
  97. },
  98. showInfoSelf(e){
  99. console.log(e.target);
  100. },
  101. demo(){
  102. //console.log('@');
  103. for(let i = 0;i < 100000; i++){
  104. console.log('#');
  105. }
  106. console.log('累坏了')
  107. }
  108. }
  109. })
  110. </script>
  111. </html>