博主信息
博文 18
粉丝 1
评论 0
访问量 20663
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
事件代理的应用和字符串方法
空瓶子
原创
838人浏览过

1. 实例演示事件代理的实现机制

事件代理: 也叫”事件委托”。如果不阻止事件冒泡,那么事件会向上传递,利用这一点可以实现事件委托。即将节点上的事件委托到其父辈上。
事件委托通常用来处理子节点事件分发和动态节点的事件监听。
demo

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <ul>
  10. <li>item1</li>
  11. <li>item2</li>
  12. <li>item3</li>
  13. <li>item4</li>
  14. <li>item5</li>
  15. </ul>
  16. </body>
  17. <script>
  18. const lis = document.querySelectorAll("li");
  19. // 遍历每个li,并逐个为它添加点击事件
  20. document.querySelector("ul").addEventListener("click", (ev) => {
  21. // 事件绑定者
  22. console.log(ev.currentTarget);
  23. // 事件触发者,通常是"事件绑定者"的子元素
  24. console.log(ev.target.innerHTML);
  25. });
  26. </script>
  27. </html>

2. 完成留言板,添加留言删除功能

demo

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>留言板</title>
  7. </head>
  8. <body>
  9. <label><input type="text" name="message" /></label>
  10. <ul id="list"></ul>
  11. <script>
  12. // 获取元素
  13. const msg = document.querySelector("input");
  14. const ul = document.querySelector("#list");
  15. msg.onkeydown = (ev) => {
  16. // 键盘事件中,key表示按下的键名
  17. // console.log(ev.key);
  18. if (ev.key === "Enter") {
  19. // 非空判断
  20. if (ev.currentTarget.value.length === 0) {
  21. alert("内容不能为空");
  22. } else {
  23. // 将留言内容添加到列表中
  24. // 创建留言
  25. let str = `<li>${
  26. ev.currentTarget.value
  27. }${"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"}${"删除"}</li>`;
  28. // 应该将最新的信息永远放在第一条
  29. list.insertAdjacentHTML("afterbegin", str);
  30. // 清空上一条留言
  31. ev.currentTarget.value = null;
  32. }
  33. }
  34. };
  35. // 删除
  36. const lis = document.querySelectorAll("li");
  37. document.querySelector("#list").addEventListener("click", (ev) => {
  38. // 事件绑定者
  39. ul.removeChild(document.querySelector("#list li"));
  40. console.log(ev.currentTarget);
  41. // 事件触发者,通常是"事件绑定者"的子元素
  42. console.log(ev.target.innerHTML);
  43. });
  44. </script>
  45. </body>
  46. </html>

3.常用字符串方法

字符串方法 功能 主要代码 返回值
concat() 把一个或者多个字符串连接起来 `` hellophp !888
slice(start, end) 从字符串中截图一段字符串 res = str.slice(0, 5); hello
substr(start, length) 从字符串中截图一段字符串 res = str.substr(0, 5); hello
trim() 去除两边空格 psw.trim(); 没有空格的字符串
split() 把字符串打成数组 res = str.split(""); 一个数组
charAt() 对字符串中指定索引处的字符进行取值 res = str.charAt(2); l
indexOf() 查询某个字符首次出现的位置 res = str.indexOf("l"); 2
replace(oldV, newV) 将匹配到的第一个旧的值替换成新的值 res = str.replace("l", "L"); heLlo php.cn
toLocaleUpperCase() 将小写全部转换成大写 res = str.toLocaleUpperCase(); HELLO PHP.CN
toLocaleLowerCase() 将大写全部转换成小写 res = str.toLocaleLowerCase(); hello php.cn

demo

  1. <script>
  2. // 把一个或者多个字符串连接起来
  3. let str = "hello".concat("php !", 888);
  4. console.log(str);
  5. str = "hello php.cn";
  6. // 从字符串中截图一段字符串
  7. let res = str.slice(0, 5);
  8. console.log(res);
  9. str = "hello php.cn";
  10. // 从字符串中截图一段字符串
  11. res = str.substr(0, 5);
  12. console.log(res);
  13. // 去除两边空格
  14. let psw = " root888 ";
  15. console.log(psw.length);
  16. psw = " root888 ";
  17. console.log(psw.trim().length);
  18. // 把字符串打成数组
  19. res = str.split("");
  20. console.log(res);
  21. // 对字符串中指定索引处的字符进行取值
  22. res = str.charAt(2);
  23. console.log(res);
  24. // 查询某个字符首次出现的位置
  25. res = str.indexOf("l");
  26. console.log(res);
  27. // 将匹配到的第一个旧的值替换成新的值
  28. res = str.replace("l", "L");
  29. console.log(res);
  30. // 将小写全部转换成大写
  31. res = str.toLocaleUpperCase();
  32. console.log(res);
  33. // 将大写全部转换成小写
  34. res = str.toLocaleLowerCase();
  35. console.log(res);
  36. </script>

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学