Blogger Information
Blog 62
fans 3
comment 1
visits 29689
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS冒泡事件和事件代理操作与字符串api
kiraseo_wwwkiraercom
Original
304 people have browsed it

冒泡事件和事件代理操作

  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>1. 事件冒泡,事件代理</title>
  8. </head>
  9. <body>
  10. <ul>
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. </ul>
  17. <script>
  18. // 事件冒泡
  19. // console.log('事件冒泡');
  20. const limao = document.querySelectorAll('li');
  21. limao.forEach( function( item){
  22. item.onclick = function (){
  23. console.log(event.currentTarget.textContent);
  24. }
  25. });
  26. document.querySelector('ul').onclick = () => console.log('li的父级ul:', event.currentTarget);
  27. </script>
  28. <ul class="list">
  29. <li data-name="item1">item1</li>
  30. <li data-name="item2">item2</li>
  31. <li data-name="item3">item3</li>
  32. <li data-name="item4">item4</li>
  33. <li data-name="item5">item5</li>
  34. </ul>
  35. <script>
  36. console.log('事件代理');
  37. const ul = document.querySelector('.list');
  38. ul.onclick = function () {
  39. console.log(event.target, event.target.dataset.name);
  40. console.log(event.currentTarget);
  41. }
  42. </script>
  43. </body>
  44. </html>

冒泡事件

事件代理

字符串API,进行演示(MDN)

代码如下

  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>字符串API,进行演示(MDN)</title>
  8. </head>
  9. <body>
  10. <ul class="xz">
  11. <li>one two three four five</li>
  12. <li>WHO IS MY NAME?</li>
  13. <li>WHO IS MY NAME?</li>
  14. <li>WHO IS MY NAME?</li>
  15. <li> WHO IS MY NAME? </li>
  16. </ul>
  17. <script>
  18. console.log('字符串API');
  19. const xz= document.querySelector(".xz");
  20. let li_one = document.querySelector(".xz li:first-of-type").textContent.toUpperCase();
  21. li_one = '<li>'+'小写转换大写-' +li_one +'</li>';
  22. xz.insertAdjacentHTML('afterbegin',li_one);
  23. let li_two = document.querySelector(".xz li:nth-of-type(2)").textContent.toLowerCase();
  24. li_two = '<li>'+ '大写转换小写-' +li_two+'</li>';
  25. xz.insertAdjacentHTML('afterbegin',li_two);
  26. let li_three = document.querySelector(".xz li:nth-of-type(3)").textContent.bold();
  27. li_three = '<li>'+ '文字加粗-' +li_three+'</li>';
  28. xz.insertAdjacentHTML('afterbegin',li_three);
  29. let li_four = document.querySelector(".xz li:nth-of-type(4)").textContent.big();
  30. li_four = '<li>'+ '文本加大-' +li_four+'</li>';
  31. xz.insertAdjacentHTML('afterbegin',li_four);
  32. let li_five = document.querySelector(".xz li:last-of-type").textContent.trim();
  33. li_five = '<li>'+ '两端删除空白字符-' +li_five+'</li>';
  34. xz.insertAdjacentHTML('afterbegin',li_five);
  35. </script>
  36. </body>
  37. </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