PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 第17章 0107-事件与常用函数,学习心得、笔记

第17章 0107-事件与常用函数,学习心得、笔记

努力工作--周工--Robin
努力工作--周工--Robin 原创
2021年01月11日 06:46:01 614浏览

今天所学心得、笔记

1、事件代理的实现机制

  1. <ul id="list">
  2. <li class="ls">item1</li>
  3. <li class="ls">item2</li>
  4. <li class="ls">item3</li>
  5. <li class="ls">item4</li>
  6. <li class="ls">item5</li>
  7. </ul>
  8. <script>
  9. // 通过事件监听器添加事件(采用事件方法函数方式),因通过回调添加的事件无法移除
  10. // 事件绑定者(ev.currentTarget),事件触发者,通常是"事件绑定者"的子元素
  11. //(ev.target)
  12. const handle = ev => console.log(ev.target.innerHTML);
  13. //给li元素添加事件
  14. let item = document.querySelector("#list");
  15. item.addEventListener("click", handle);
  16. //移除事件
  17. // item.removeEventListener("click", handle);
  18. </script>

1、留言板,添加留言,删除功能

  1. let msg = document.querySelector("input");
  2. let msgList = document.querySelector("#msgList");
  3. // 留言板,升级版(添加、删除留言功能)
  4. let msgArr = []; //留言数据的数组
  5. msg.onkeydown = ev => {
  6. if(ev.key === "Enter"){
  7. if(ev.currentTarget.value.length === 0){
  8. alert("留言内容不能为空");
  9. }else {
  10. msgArr.push(ev.currentTarget.value);
  11. // console.log(msgArr);
  12. load();
  13. // 清空input框内容
  14. ev.currentTarget.value = null;
  15. }
  16. }
  17. }
  18. // 渲染数据到页面
  19. function load() {
  20. let li = document.querySelectorAll(".li");
  21. // 渲染数据到页面前,删除所有li
  22. for (i=0; i<li.length; i++) {
  23. msgList.removeChild(li[i]);
  24. }
  25. console.log(msgArr);
  26. for(i=0; i<msgArr.length; i++) {
  27. //在这里使用 button 标签不行,没有冒泡效果,最后只能用 input标签代替,
  28. //不知道为什么???
  29. let str = `<li class="li"><div class="box">
  30. <span>${msgArr[i]}</span><input type="button" class="btn" id="${i}" value="删除">
  31. </div></li>`;
  32. //添加留言内容到页面
  33. msgList.insertAdjacentHTML("afterbegin", str);
  34. }
  35. }
  36. // 删除指定的留言数据,并重新渲染数据到页面
  37. msgList.addEventListener("click", function (ev) {
  38. msgArr.splice(ev.target.id,1);
  39. load();
  40. });

代码功能截图

1、字符串方法

  1. // 1. concat()拼装
  2. let str = "html".concat(" css ", "www.", "php.", "cn ", 888);
  3. console.log(str);
  4. str = "www.PHP.cn";
  5. // 2. slice(start, end):取子串
  6. let res = str.slice(0, 3);
  7. console.log(res);
  8. res = str.slice(3, -2);
  9. console.log(res);
  10. // 3. substr(start, length):取子串
  11. res = str.substr(3, 5);
  12. console.log(res);
  13. // 4. indexOf()返回字符串中匹配子串的第一个字符的下标
  14. res = str.indexOf("p");
  15. console.log(res);
  16. // 5. 将字符串打成数组
  17. res = str.split("");
  18. console.log(res);
  19. // 比如说从邮箱中解析出用户和和邮箱地址
  20. res = "admin@php.cn".split("@");
  21. console.log(res[0]);
  22. console.log(res[1]);
  23. // 6. join()指定的分隔符将一个数组合并为一个字符串
  24. var arr= ["jpg","bmp","gif","ico","png"];
  25. res = arr.join("|");
  26. console.log(res);
  27. // 7. charAt()返回指定位置的字符,下标由0开始;
  28. res = str.charAt(5);;
  29. console.log(res);
  30. // 8. toLowerCase()转小写
  31. res = str.toLowerCase();;
  32. console.log(res);
  33. // 9. toUpperCase()转大写
  34. res = str.toUpperCase();;
  35. console.log(res);
  36. // 10. trim():删除二端空格
  37. pwd = " root888 ";
  38. console.log(pwd.trim().length);
  39. // 11. search() 搜索特字符串,返回匹配位置
  40. res = str.search("PHP");
  41. console.log(res);
  42. // 12. replace() 替换在字符串中指定的值
  43. res = str.replace("PHP", "JAVA");
  44. console.log(res);
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议