当前位置:   article > 正文

vue.js(2):如何阻止事件传播和默认行为_vue阻止默认事件

vue阻止默认事件

1.事件冒泡

IE提出的事件流是事件冒泡流,这种事件流传播是由内向外进行传递。即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档),如下图:

 

下面演示VUE.JS中的事件冒泡,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  7. <script>
  8. window.onload=function(){
  9. new Vue({
  10. el:"#root",
  11. methods:{
  12. showA:function(){
  13. console.log("执行A方法");
  14. },
  15. showB:function(){
  16. console.log("执行B方法");
  17. },
  18. showC:function(){
  19. console.log("执行C方法");
  20. }
  21. }
  22. })
  23. }
  24. </script>
  25. <style>
  26. .a{
  27. border: #FF6600 solid 1px;
  28. width: 300px;
  29. }
  30. .b{
  31. border: #FF6600 solid 1px;
  32. width: 200px;
  33. }
  34. .c{
  35. border: #FF6600 solid 1px;
  36. width: 100px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="root">
  42. <!--阻止事件冒泡-->
  43. <div @click="showA()" class="a">A
  44. <div @click="showB()" class="b">B
  45. <div @click="showC()" class="c">C</div>
  46. </div>
  47. </div>
  48. </div>
  49. </body>
  50. </html>

点击C区域,查看控制台输出,会发现,同时促发了外部B区域和A区域的方法;

 

 

下面添加一个C stop区域,演示阻止事件冒泡,在该区域的click方法上添加一个stop属性,如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  7. <script>
  8. window.onload=function(){
  9. new Vue({
  10. el:"#root",
  11. methods:{
  12. showA:function(){
  13. console.log("执行A方法");
  14. },
  15. showB:function(){
  16. console.log("执行B方法");
  17. },
  18. showC:function(){
  19. console.log("执行C方法");
  20. }
  21. }
  22. })
  23. }
  24. </script>
  25. <style>
  26. .a{
  27. border: #FF6600 solid 1px;
  28. width: 300px;
  29. }
  30. .b{
  31. border: #FF6600 solid 1px;
  32. width: 200px;
  33. }
  34. .c{
  35. border: #FF6600 solid 1px;
  36. width: 100px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="root">
  42. <!--阻止事件冒泡-->
  43. <div @click="showA()" class="a">A
  44. <div @click="showB()" class="b">B
  45. <div @click="showC()" class="c">C</div>
  46. <div @click.stop="showC()" class="c">C stop</div>
  47. </div>
  48. </div>
  49. </div>
  50. </body>
  51. </html>

 

点击C stop区域查看效果,如下图,事件冒泡已阻止:

 

2.默认行为

很多标签都有默认行为,在点击以后触发默认行为,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  7. <script>
  8. window.onload=function(){
  9. new Vue({
  10. el:"#root",
  11. methods:{
  12. showA:function(){
  13. console.log("执行A方法");
  14. },
  15. showB:function(){
  16. console.log("执行B方法");
  17. },
  18. showC:function(){
  19. console.log("执行C方法");
  20. }
  21. }
  22. })
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <div id="root">
  28. <!--阻止默认行为-->
  29. <a href="http://www.baidu.com" @click="showA()">clickA</a>
  30. </div>
  31. </body>
  32. </html>

运行以上代码会发现,点击完标签以后,会先触发点击事件,然后跳转页面。

下面添加一个新的a标签,演示阻止默认行为,在该区域的click方法上添加一个prevent属性,如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  7. <script>
  8. window.onload=function(){
  9. new Vue({
  10. el:"#root",
  11. methods:{
  12. showA:function(){
  13. console.log("执行A方法");
  14. },
  15. showB:function(){
  16. console.log("执行B方法");
  17. },
  18. showC:function(){
  19. console.log("执行C方法");
  20. }
  21. }
  22. })
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <!--阻止事件冒泡-->
  28. <div id="root">
  29. <!--阻止默认行为-->
  30. <a href="http://www.baidu.com" @click="showA()">clickA</a>
  31. <a href="http://www.baidu.com" @click.prevent="showA()">clickA</a>
  32. </div>
  33. </body>
  34. </html>

点击以后,不触发默认事件。

 

3.只执行一次

在点击事件的后面加行once属性,该事件只会运行一次,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  7. <script>
  8. window.onload=function(){
  9. new Vue({
  10. el:"#root",
  11. methods:{
  12. showA:function(){
  13. console.log("执行A方法");
  14. },
  15. showB:function(){
  16. console.log("执行B方法");
  17. },
  18. showC:function(){
  19. console.log("执行C方法");
  20. }
  21. }
  22. })
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <!--阻止事件冒泡-->
  28. <div id="root">
  29. <!--只执行一次 addEventListener removeEventListener-->
  30. <button @click.once="showB()">once</button>
  31. </div>
  32. </body>
  33. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/79388
推荐阅读
相关标签
  

闽ICP备14008679号