Blogger Information
Blog 36
fans 1
comment 0
visits 26106
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
事件冒泡和事件代理的原理 字符串常用API
早晨
Original
417 people have browsed it

事件冒泡和事件代理的原理

事件冒泡就是从最深处的节点一层一层向外执行,假如有一个节点树div > ul > li > a,给a标签绑定一个click事件,它的执行是从a < li < ul < div,逐步向外执行,这中机制就叫事件冒泡。
1、事件流触发事件时,事件传递的过程
2、事件流三个阶段:事件捕获/事件目标/事件冒泡
3、事件捕获:事件由祖先元素依次向子孙元素传递事件的过程
4、事件冒泡:事件目标向祖先元素传递事件的过程

事件代理是通过事件冒泡的原理来实现的,假如给div节点添加click点击事件, 里面的子节点都会触发点击事件,这种机制就是事件代理。事件代理是利用事件冒泡的原理将子级的事件代理给父级,有两个好处:一可以减少事件的绑定 提高性能;二可以可以实现对未来元素的事件绑定。

实例演示代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. </head>
  9. <body>
  10. <!-- 事件冒泡案例 -->
  11. <ul>
  12. <li>事件冒泡1</li>
  13. <li>事件冒泡2</li>
  14. </ul>
  15. <script>
  16. const mb = document.querySelectorAll("li");
  17. mb.forEach(function (con) {
  18. con.onclick = function () {
  19. console.log(event.currentTarget.textContent);
  20. console.log("事件主体: ", event.currentTarget);
  21. console.log("事件目标: ", event.target);
  22. console.log(event.target === event.currentTarget);
  23. };
  24. });
  25. </script>
  26. -----------------------------------------------------------------------
  27. <!-- 事件代理案例 -->
  28. <ul class="list">
  29. <li data-dl="1">事件代理1</li>
  30. <li data-dl="2">事件代理2</li>
  31. </ul>
  32. <script>
  33. const dl = document.querySelector(".list");
  34. dl.onclick = function () {
  35. console.log(event.target, event.target.dataset.dl);
  36. console.log(event.currentTarget.textContent);
  37. };
  38. </script>
  39. </body>
  40. </html>

运行效果

字符串常用API

1.toLowerCase() toUpperCase() 转换大小写,返回新的字符串;
2.charCodeAt(index)根据索引获取字符串中的某一个字符的ASCII编码;
3.startsWith() endsWith() 判断一个字符串是否以某个字符或字符串开头/结尾,返回布尔值;
4.includes(str,stratIndex)判断一个字符串中是否包含某个字符串,返回布尔值;
5.trim() 去除字符串两端的空格;
6.split(’/’) 以某一特殊字符,将字符串分割为数组;
7.substr(startindex,lenght) 截取字符串;
8.charAt(index) 根据索引获取字符串中的某一个字符;
9.replace(oldstr,newstr) 替换字符串中的某一部分,返回新的字符串;
10.indexOf() 查询指定字符在字符串中第一次出现的索引。

实例演示代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>字符串中的常用api</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 转换大小写
  12. let gg = "hello world";
  13. console.log(gg.toLocaleLowerCase());
  14. console.log(gg.toLocaleUpperCase());
  15. console.log("--------------------------------------------------");
  16. // 获取字符串中的某一个字符
  17. console.log(gg.charCodeAt(1));
  18. console.log("--------------------------------------------------");
  19. // 判断一个字符串是否以某个字符或字符串开头/结尾
  20. console.log(gg.startsWith("hello"));
  21. console.log(gg.endsWith("ld"));
  22. console.log("--------------------------------------------------");
  23. // 判断一个字符串中是否包含某个字符串
  24. console.log(gg.includes("hello"));
  25. let gg1 = "早上好,下午好,晚上好";
  26. console.log(gg1.includes("早上好", 5));
  27. console.log("--------------------------------------------------");
  28. // 去除字符串两端空格
  29. let gg2 = " 早 上 好 ! ";
  30. console.log(gg2);
  31. console.log(gg2.trim());
  32. console.log("--------------------------------------------------");
  33. // 以某一特殊字符,将字符串分割为数组
  34. let gg3 = "中国/美国/韩国";
  35. console.log(gg3.split("/"));
  36. console.log("--------------------------------------------------");
  37. </script>
  38. </body>
  39. </html>

运行效果

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!