Blogger Information
Blog 35
fans 0
comment 0
visits 17008
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
事件冒泡和事件代理、字符串API
三九三伏
Original
283 people have browsed it

一、事件冒泡和事件代理

1. 事件冒泡:

元素事件的触发,默认会向上(父级)传递,当父级元素有同名事件时,会被自动触发,这种传递默认可以传至window。

  1. ...
  2. <body>
  3. ...
  4. <ul>
  5. <li>item1</li>
  6. <li>item2</li>
  7. <li>item3</li>
  8. <li>item4</li>
  9. <li>item5</li>
  10. </ul>
  11. <script>
  12. const items = document.querySelectorAll('li');
  13. console.log(items);
  14. items.forEach((item)=>(item.onclick = ()=>{
  15. console.log(event);
  16. //事件目标
  17. console.log(event.target);
  18. //事件主体
  19. console.log(event.currentTarget);
  20. }));
  21. document.querySelector('ul').onclick = (()=>console.log('li的父级ul:',event.currentTarget));
  22. document.body.onclick = (()=>console.log('ul的父级body:',event.currentTarget));
  23. document.documentElement.onclick = (()=>console.log('body的父级html:',event.currentTarget));
  24. document.onclick = (()=>console.log('html的父级document:',event.currentTarget));
  25. window.onclick = (()=>console.log('document的父级window:',event.currentTarget));
  26. </script>
  27. ...
  28. </body>

点击item1,效果如下:

阻止事件冒泡

  1. ...
  2. <body>
  3. ...
  4. <script>
  5. ...
  6. //阻止冒泡
  7. event.stopPropagation();
  8. ...
  9. </script>
  10. ...
  11. </body>

点击li元素将不再有事件冒泡

2.事件委托/事件代理

将子元素事件委托在父元素的事件上触发,以简化代码。在事件代理/委托时,事件目标(target)和事件主体(currentTarget)是不一样的。

  1. ...
  2. <body>
  3. ...
  4. <ul>
  5. <!-- 自定义属性data-index是为了便于区分事件的触发者 -->
  6. <li data-index="1">item1</li>
  7. <li data-index="2">item2</li>
  8. <li data-index="3">item3</li>
  9. <li data-index="4">item4</li>
  10. <li data-index="5">item5</li>
  11. </ul>
  12. <script>
  13. const ul = document.querySelector('ul');
  14. ul.onclick = ()=>{
  15. // target事件目标li,子元素,dataset.index引用li中data-index的值
  16. console.log('event.target:',event.target,event.target.dataset.index);
  17. // currentTarget事件主体ul,onclick是绑定在父元素ul上的
  18. console.log('event.currentTarget:',event.currentTarget);
  19. };
  20. </script>
  21. ...
  22. </body>

选择点击li效果如下:

二、 字符串API示例

  1. ...
  2. <body>
  3. ...
  4. <script>
  5. let str = 'php中文网';
  6. //转成大写
  7. console.log(str.toUpperCase());
  8. // 转成字符数组
  9. const arr = str.split('');
  10. console.log(str.split(''));
  11. console.log(arr[0],arr[4]);
  12. // 搜索
  13. console.log(str.startsWith('p')); //以“p”开头
  14. console.log(str.endsWith('网')); //以“网”结尾
  15. //处理
  16. console.log(str.slice(-3)); //保留倒数三个字符
  17. console.log(str.slice(-3).padStart(str.length,'*')); //从0-倒数第三个之间用*遮盖
  18. console.log(str.length);
  19. console.log(str.charAt(2));
  20. console.log(str.indexOf('中'));
  21. console.log(str.concat('(','https://php.cn',')'));
  22. console.log(str.replace('中文网','cn'));
  23. console.log(str.slice(0,3));
  24. console.log(str.substr(0,4));
  25. console.log(str.substr(-3,3));
  26. </script>
  27. ...
  28. </body>

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