博主信息
博文 26
粉丝 0
评论 0
访问量 21005
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
事件与常用函数
庄周梦蝶
原创
898人浏览过

事件代理的实现机制

  1. <body>
  2. <button>
  3. button1
  4. </button>
  5. <button>
  6. button2
  7. </button>
  8. <button>
  9. button3
  10. </button>
  11. <script>
  12. const but2=document.querySelector("body button:nth-of-type(2)");
  13. //通过onclick来添加事件,不能重复给一个变量添加事件,后面的会覆盖前面的
  14. bth2.onclick=function(){
  15. this.style.color="red";
  16. };
  17. //移除事件
  18. bth2.onclick=null
  19. //通过事件监听器来添加事件,用事件监听器给同一个变量添加事件不会覆盖,会依次执行
  20. const bth3=document.querySelector("body button:nth-of-type(3)");
  21. bth3.addEventListener("click",function(){
  22. console.log(this.innerHTML,"第一次");
  23. })
  24. bth3.addEventListener("click",function(){
  25. console.log(this.innerHTML,"第二次");
  26. });
  27. //普通移除无法移除事件监听器
  28. //通过吧事件函数给一个独立变量,然后事件派发给到需要的地方
  29. const handle=()=>console.log(bth3.innerHTML,"第3次");
  30. bth3.addEventListener("click",handle);
  31. //删除
  32. btn3.removeEventListener("click", handle);
  33. const btn4 = document.querySelector("body button:nth-of-type(4)");
  34. //事件传递就两种,捕获:从外到内,冒泡从内到外
  35. </script>
  36. </body>

留言板

  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>Document</title>
  8. </head>
  9. <body>
  10. <label><input type="text" name="message" /></label>
  11. <ol id="list"></ol>
  12. <script>
  13. //获取元素
  14. const msg = document.querySelector("input");
  15. const list = document.querySelector("#list");
  16. //把文本框里面的东西弄到下面
  17. msg.onkeydown = (ev) => {
  18. //键盘事件中,key表示按下的键名
  19. // console.log(ev.key);
  20. // 判断是否输出为enter;
  21. if (ev.key === "Enter") {
  22. //非空判断
  23. if (ev.currentTarget.value.length === 0) {
  24. alert("内容不能为空");
  25. } else {
  26. //讲留言添加到列表中
  27. //创建留言
  28. //文本框里面value就是框里面填写的内容
  29. let str = `<li>${ev.currentTarget.value}<button>删除</button></li>`;
  30. //留言永远是最新的,所以要到第一天
  31. //这个支持html字符串里面有标签,afterbegin是添加到起始符后面
  32. list.insertAdjacentHTML("afterbegin", str);
  33. //清空文本框内上一条留言
  34. ev.currentTarget.value = "";
  35. const ol = document.querySelector("body ol");
  36. const bu = document.querySelectorAll("body button");
  37. for (let i = 0; i < bu.length; i++) {
  38. bu[i].onclick = function () {
  39. //删除当前的父级,按钮的父级就是li,
  40. ol.removeChild(this.parentNode);
  41. };
  42. }
  43. }
  44. }
  45. };
  46. </script>
  47. </body>
  48. </html>


删除第二条留言

字符串方法练习

  1. <script>
  2. //1.concat(),字符串拼接,会自动类型转换,
  3. let str = "html".concat(" css ", " php! ", 888);
  4. console.log(str);
  5. //2.slice(起始start,结束end)取子串
  6. str = "hello php.cn";
  7. res = str.slice(0, 5);
  8. console.log(res);
  9. //substr(开始,获取的数值长度)
  10. res = str.substr(0, 5);
  11. console.log(res);
  12. //4.teim()删除两端空格
  13. let psw = " root888 ";
  14. console.log(psw.length);
  15. console.log(psw.trim().length);
  16. //5.把字符串打成数组,字符串中间的空格也算
  17. res = str.split("");
  18. console.log(res);
  19. //用法,括号里面填写就字符串就是从这个开始分成两份
  20. res = "admin@qq.com".split("@");
  21. console.log(res);
  22. //6看字符某一位
  23. console.log(str.charAt(0));
  24. </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+教程免费学