Blogger Information
Blog 41
fans 0
comment 0
visits 41334
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS事件代理的实现机制|JS留言板添加删除|JS字符串常用方法
幸福敲门的博客
Original
714 people have browsed it
  1. 实例演示事件代理的实现机制;
  2. 完成留言板,添加留言删除功能
  3. 自选不少于10个字符串方法进行练习
  4. 预习常用的数组方法
    一、事件代理的实现机制
    1.1元素的事件属性
    1. <body>
    2. <!-- 1. 添加到元素的事件属性上 -->
    3. <button ">Button1</button>
    4. <button >Button2</button>
    5. <button onclick="console.log(this.innerHTML)>Button3</button>
    6. <button>点击广告</button>
    7. </body>
    图示:
    元素的事件属性
    1.2通过脚本添加事件属性
    1. <body>
    2. <button >Button1</button>
    3. <button >Button2</button>
    4. <button >Button3</button>
    5. <button>点击广告</button>
    6. </body>
    7. <script>
    8. const btn3 = document.querySelector("body button:nth-of-type(3)");
    9. // 添加事件
    10. btn3.onclick = function () {
    11. console.log(this.innerHTML);
    12. };
    13. btn3.onclick = function () {
    14. this.style.color = "green";
    15. };
    16. </script>
    图示:
    脚本添加事件

1.3通过事件监听器添加事件

  1. <body>
  2. <button >Button1</button>
  3. <button >Button2</button>
  4. <button >Button3</button>
  5. <button>点击广告</button>
  6. </body>
  7. <script>
  8. const btn2 = document.querySelector("body button:nth-of-type(2)");
  9. btn2.addEventListener("click", function () {
  10. console.log(this.innerHTML, "第1次");
  11. });
  12. btn2.addEventListener("click", function () {
  13. console.log(this.innerHTML, "第2次");
  14. });
  15. btn2.addEventListener("click", function () {
  16. console.log(this.innerHTML, "第3次");
  17. });
  18. btn2.addEventListener("click", function () {
  19. console.log(this.innerHTML, "第4次");
  20. });
  21. </script>

图示:
事件监听器添加事件
1.4事件派发(广告点击计费)

  1. <script>
  2. const btn4 = document.querySelector("body button:nth-of-type(4)");
  3. const ev = new Event("click");
  4. let i = 0;
  5. btn4.addEventListener("click", function () {
  6. console.log("点击了广告, 共计: " + i + "元");
  7. i += 1.3;
  8. });
  9. // btn4.dispatchEvent(ev);
  10. // 使用间歇式定时器来自动点击广告
  11. setInterval("btn4.dispatchEvent(ev)", 1000);
  12. </script>

广告点击计费

二、留言板添加删除

  1. <body>
  2. <label><input type="text" name="message" /></label>
  3. <ol id="list"></ol>
  4. <script>
  5. // 获取元素
  6. const msg = document.querySelector("input");
  7. const list = document.querySelector("#list");
  8. msg.onkeydown = ev => {
  9. // 键盘事件中,key表示按下的键名
  10. // console.log(ev.key);
  11. if (ev.key === "Enter") {
  12. // 非空判断
  13. if (ev.currentTarget.value.length === 0) {
  14. alert("内容不能为空");
  15. } else {
  16. // 将留言内容添加到列表中
  17. // 创建留言
  18. let str = `<li>${ev.currentTarget.value}</li>`;
  19. // 应该将最新的信息永远放在第一条
  20. list.insertAdjacentHTML("afterbegin", str);
  21. // 清空上一条留言
  22. ev.currentTarget.value = null;
  23. }
  24. }
  25. };
  26. </script>
  27. </body>

图示:
添加留言

删除留言:

  1. <body>
  2. <label>请留言:&nbsp;<input type="text" name="message" /></label>
  3. <ol id="list"></ol>
  4. <script>
  5. // 获取元素
  6. const msg = document.querySelector("input");
  7. const list = document.querySelector("#list");
  8. msg.onblur = ev => {
  9. console.log(ev.currentTarget);
  10. };
  11. msg.onkeydown = ev => {
  12. // ev.key: 键盘事件中,key表示按下的键名
  13. // console.log(ev.key);
  14. if (ev.key === "Enter") {
  15. // 非空判断
  16. if (ev.currentTarget.value.length === 0) {
  17. alert("内容不能为空");
  18. } else {
  19. // 创建包括留言内容的html字符串
  20. let str = `<li>${ev.currentTarget.value}<button onclick="del(this)">删除</button></li>`;
  21. // 最新留言应该始终做为第一条插入
  22. list.insertAdjacentHTML("afterbegin", str);
  23. // 请空上一条留言
  24. ev.currentTarget.value = null;
  25. }
  26. }
  27. };
  28. // 删除留言
  29. function del(ele) {
  30. return confirm("是否删除?") ? (ele.parentNode.outerHTML = null) : false;
  31. }
  32. </script>
  33. </body>

图示:
删除留言

三、字符串常用方法
3.1 concat()拼装

  1. <script>
  2. // 1. concat()拼装
  3. let str = "student".concat("name", "age","sex", "school", "height");
  4. console.log(str);
  5. str ="student name age sex school height";
  6. console.log(str);
  7. </script>

图示:
concat()拼装
3.2 slice(start, end):取子串

  1. <script>
  2. let str = "welcome to beijing";
  3. let res = str.slice(0, 8);
  4. console.log(res);
  5. res = str.slice(0);
  6. console.log(res);
  7. res = str.slice(7);
  8. console.log(res);
  9. res = str.slice(-7);
  10. console.log(res);
  11. res = str.slice(-4, 10);
  12. console.log(res);
  13. // 正方向从0开始,反方向从-1开始
  14. res = str.slice(-8, -4);
  15. console.log(res);
  16. </script>

图示:
取子串

3.3 substr(start, length)截取子串

  1. <script>
  2. let str = "welcome to beijing";
  3. str=str.substr(6, 8);
  4. console.log(str);
  5. </script>

截取字串

3.4trim():删除二端空格

  1. <script>
  2. let psw = " admin123456 ";
  3. console.log(psw.length);
  4. psw = " admin123456 ";
  5. console.log(psw.trim().length);
  6. </script>

图示:
删除二端空格

3.5将字符串打成数组

  1. <script>
  2. let res = "chinamade|Beautiful hometown".split("|");
  3. console.log(res[0]);
  4. </script>

图示:
将字符串打成数组

3.6用repeat多次复制字符串

  1. <script>
  2. let str = "china.made";
  3. str = str.repeat(3);
  4. console.log(str);
  5. </script>

图示:
repeat多次复制字符串

3.7填充一个字符串到指定的长度

  1. <script>
  2. let str = "china.made";
  3. str = '001'.padStart(8, '0')
  4. console.log(str);
  5. </script>

图示:
填充

3.8length计算字符串中的字符

  1. <script>
  2. const word = "china.made";
  3. console.log(word.length)
  4. </script>

图示:
计算长度

3.9(…)、Array.reverse和Array.join方法反转字符串中的字符

  1. <script>
  2. const word = "china.made";
  3. const reversedWord = [...word].reverse().join("")
  4. console.log(reversedWord)
  5. </script>

图示:
反转字符串

3.10用String.startsWith和String.endsWith检查字符串是否以特定序列开头或结尾

  1. <script>
  2. const word = "china.made!beautiful girl!"
  3. console.log(word.startsWith("china")); // true
  4. console.log(word.endsWith("made")); // false
  5. </script>

图示:
特定序列开头或结尾

3.11search() 搜索子串

  1. <script>
  2. let str = '';
  3. console.log(str.search(/5c\-/));
  4. </script>

图示:
搜索子串

Correcting teacher:天蓬老师天蓬老师

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