005-事件捕获、冒泡&事件委托

事件捕获、冒泡&事件委托

1、事件捕获与冒泡

2、事件冒泡示例

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        width: 300px;
        height: 200px;
        border: 1px solid #ccc;
        background: #f5f5f5;
      }
      .son {
        width: 100px;
        height: 100px;
        background: pink;
        text-align: center;
        line-height: 90px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="son">son</div>
    </div>
  </body>
  <script>
    const son = document.getElementsByClassName('son')[0];
    const parent = document.getElementsByClassName('parent')[0];
    son.onclick = function (e) {
      console.log('son click');
    };
    parent.onclick = function (e) {
      console.log('parent click');
    };
  </script>
</html>

当在页面触发 son 元素的点击事件时,因为事件冒泡,会依次打印 son click、parent click。

3、阻止事件冒泡

javascript 复制代码
const son = document.getElementsByClassName('son')[0];
const parent = document.getElementsByClassName('parent')[0];
son.onclick = function (e) {
  console.log('son click');
  e.stopPropagation();  // 阻止事件冒泡
};
parent.onclick = function (e) {
  console.log('parent click');
};

当在页面触发 son 元素的点击事件时,只会打印 son click。

4、阻止事件默认行为

💡 Tips:e.preventDefault() 或 return false 会阻止默认行为

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <a href="http://www.baidu.com">百度</a>
  </body>
  <script>
    const aDom = document.getElementsByTagName('a')[0];
    aDom.onclick = function (e) {
      console.log('a click');
      e.preventDefault(); // 阻止默认跳转
      // return false;  // 阻止默认跳转
    };
  </script>
</html>

5、事件委托

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
  </body>
  <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
  <script>
    // 将 li 的点击事件注册到 ul 上
    $('ul').on('click', 'li', function () {
      console.log(this); // <li>x</li>
    });
  </script>
</html>

6、事件委托优点

相关推荐
江城开朗的豌豆5 分钟前
forEach遇上await:你的异步代码真的在按顺序执行吗?
前端·javascript·面试
万少13 分钟前
HarmonyOS Next 弹窗系列教程(3)
前端·harmonyos·客户端
七灵微1 小时前
【后端】单点登录
服务器·前端
持久的棒棒君5 小时前
npm安装electron下载太慢,导致报错
前端·electron·npm
crary,记忆7 小时前
Angular微前端架构:Module Federation + ngx-build-plus (Webpack)
前端·webpack·angular·angular.js
漂流瓶jz8 小时前
让数据"流动"起来!Node.js实现流式渲染/流式传输与背后的HTTP原理
前端·javascript·node.js
SamHou08 小时前
手把手 CSS 盒子模型——从零开始的奶奶级 Web 开发教程2
前端·css·web
我不吃饼干8 小时前
从 Vue3 源码中了解你所不知道的 never
前端·typescript
开航母的李大8 小时前
【中间件】Web服务、消息队列、缓存与微服务治理:Nginx、Kafka、Redis、Nacos 详解
前端·redis·nginx·缓存·微服务·kafka
Bruk.Liu8 小时前
《Minio 分片上传实现(基于Spring Boot)》
前端·spring boot·minio