Js、Vue阻止事件冒泡行为

忘是亡心i 2024-04-07 15:23 207阅读 0赞

目录

Js阻止事件冒泡行为

Vue阻止事件冒泡行为


以下是Js阻止事件冒泡行为 event.stopPropagation()

  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>js阻止事件冒泡行为</title>
  8. <style>
  9. #outterDiv{
  10. width: 500px;
  11. height: 500px;
  12. background:red;
  13. overflow: hidden;
  14. }
  15. #innerDiv{
  16. width: 200px;
  17. height: 200px;
  18. background:green;
  19. margin: 150px auto;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="outterDiv">
  25. <div id="innerDiv"></div>
  26. </div>
  27. </body>
  28. <script>
  29. var outter = document.querySelector("#outterDiv")
  30. var innerer = document.querySelector("#innerDiv")
  31. outter.onclick=function(){
  32. console.log("外层div outter事件触发了");
  33. }
  34. innerer.onclick=function(){
  35. console.log("内层div innerer事件触发了");
  36. event.stopPropagation() //阻止事件冒泡
  37. }
  38. </script>
  39. </html>

以下是Vue阻止事件冒泡行为 event.stopPropagation()

  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>vue阻止冒泡行为</title>
  8. <script src="../vue库/vue.js"></script>
  9. <style>
  10. #outterDiv{
  11. width: 500px;
  12. height: 500px;
  13. background:red;
  14. overflow: hidden;
  15. }
  16. #innerDiv{
  17. width: 200px;
  18. height: 200px;
  19. background:green;
  20. margin: 150px auto;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <!--
  26. Vue组件事件冒泡有两种 注:在子级标签中阻止
  27. @click.stop="inner"
  28. event.stopPropagation()
  29. -->
  30. <div id="app">
  31. <div id="outterDiv" @click="outter">
  32. <div id="innerDiv" @click.stop="inner"></div>
  33. </div>
  34. </div>
  35. </body>
  36. <script>
  37. var app = new Vue({
  38. el:"#app",
  39. methods: {
  40. outter(){
  41. console.log("外层div outter事件触发了");
  42. },
  43. inner(){
  44. console.log("内层div outter事件触发了");
  45. // event.stopPropagation()
  46. }
  47. },
  48. })
  49. </script>
  50. </html>

发表评论

表情:
评论列表 (有 0 条评论,207人围观)

还没有评论,来说两句吧...

相关阅读

    相关 阻止事件冒泡

    事件的冒泡(Bubble) \- 所谓的冒泡指的就是事件的向上传导,当后代元素上的事件被触发时,其祖先元素的相同事件也会被触发 \- 在开发中大部分情况冒泡都是有用的,如果