Blogger Information
Blog 45
fans 3
comment 0
visits 45642
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript事件与字符串常用函数
残破的蛋蛋
Original
854 people have browsed it

JavaScript事件与字符串常用函数

一、JavaScript事件基础

1.事件的三种添加方法

1.1 添加到元素的事件属性上。

  1. <button onclick="console.log(this.innerHTML)">Button1</button>

1.2 通过脚本添加到事件属性上。

  1. <button>Button2</button>
  2. <script>
  3. const btn2 = document.querySelector('body button:nth-of-type(2)');
  4. // 添加事件
  5. btn2.onclick = () => {
  6. console.log(this); // <button></button>
  7. console.log(this.innerHTML); // Button2
  8. }
  9. </script>

这里有一点需要注意的是,onclick不能重复定义一个事件,因为后面的事件会覆盖前面的事件,类似于CSS样式覆盖这个道理。

如果想要移除一个事件,可以使用以下方法:

  1. btn2.onclick = null;

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

  • 语法:

addEventListener(事件类型, 事件回调方法, 触发阶段)

  1. <button>Button3</button>
  2. <script>
  3. const btn3 = document.querySelector('button');
  4. btn3.addEventerListener('click', function () {
  5. console.log(this.innerHTML, '第一次'); // Button3 第一次
  6. });
  7. btn3.addEventListener('click', function () {
  8. console.log(this.innerHTML, "第2次"); // Button3 第二次
  9. });
  10. </script>

1.3.1事件移除

通过回调函数添加的事件是无法移除的,但是可以通过事件方法函数来移除。

  1. let handle = () => console.log(btn3.innerHTML, "第3次");
  2. // 添加点击事件
  3. btn3.addEventListener('click', handle); // Button3 第三次
  4. // 移除点击事件
  5. btn3.removeEventListener('click', handle);

1.3.2事件派发

创建一个按钮,并拿到这个元素:

  1. <button>Button4</button>
  2. <script>
  3. const btn4 = document.querySelector('button');
  4. </script>

通过事件派发,结合定时器,我们可以做一个自动点击广告的小案例。

  1. // 创建一个自定义事件
  2. let ev = new Event('click');
  3. let i = 0;
  4. btn4.addEventerListener('click', function () {
  5. console.log(`点击了广告,共赚:${i}元`);
  6. i += 0.5;
  7. });
  8. setInterval(btn4.dispatchEvent(ev), 1000);

2.事件传递

  1. 捕获:从最外层元素逐级向内知道事件的绑定者;
  2. 目标:到达目标事件;
  3. 冒泡:从目标再由内向外逐级向上知道最外层元素。

如果需要理解捕获、目标,冒泡需要先理解什么是事件的对象、事件的绑定者以及事件的触发者,现有如下案例:

以下是定义个一个ul列表的DOM结构:

  1. <ul>
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. <li class="item">item4</li>
  6. <li class="item">item5</li>
  7. </ul>

给每一个li元素绑定一个点击事件:

  1. const lis = document.querySelectorAll('.item');
  2. lis.forEach(li => (li.onclick = ev => {
  3. // 事件对象保存着当前事件的所有信息
  4. console.log(ev);
  5. // 事件类型
  6. console.log(ev.type); // click
  7. // 事件绑定者
  8. console.log(ev.currentTarget);
  9. // 事件触发者
  10. console.log(ev.target);
  11. // 在这种情况下,事件绑定者和触发者都是同一个
  12. console.log(ev.currentTarget === ev.target);
  13. // 事件的传递路径,假定:点击了item5,此时事件传递的路径就是从内向外逐级传递。
  14. console.log(ev.path); // [li.item, ul, body, html, document, Window]
  15. }));

注意:on + event事件是不支持捕获的,只支持冒泡:

  1. // 不支持捕获,仅支持冒泡
  2. li.onclick = function () {};

addEventListener的第三个参数为true时,表示事件在捕获阶段发生,false在冒泡阶段发生,也是默认事件。

  • 捕获
  1. // window
  2. window.addEventListener('click', ev => {
  3. console.log(ev.currentTarget);
  4. }, true);
  5. // document
  6. document.addEventListener('click', ev => {
  7. console.log(ev.currentTarget);
  8. }, true);
  9. // html
  10. document.documentElement.addEventListener('click', ev => {
  11. console.log(ev.currentTarget);
  12. }, true);
  13. // body
  14. document.body.addEventListener('click', ev => {
  15. console.log(ev.currentTarget);
  16. }, true);
  17. // ul
  18. document.querySelector("ul").addEventListener('click', ev => {
  19. console.log(ev.currentTarget);
  20. }, true);
  • 效果图:

捕获方向

  • 冒泡
  1. // ul
  2. document.querySelector("ul").addEventListener('click', ev => {
  3. console.log(ev.currentTarget);
  4. }, false);
  5. // body
  6. document.body.addEventListener('click', ev => {
  7. console.log(ev.currentTarget);
  8. }, false);
  9. // html
  10. document.documentElement.addEventListener('click', ev => {
  11. console.log(ev.currentTarget);
  12. }, false);
  13. // document
  14. document.addEventListener('click', ev => {
  15. console.log(ev.currentTarget);
  16. }, false);
  17. // window
  18. window.addEventListener('click', ev => {
  19. console.log(ev.currentTarget);
  20. }, false);
  • 效果图:

冒泡

从上述捕获和冒泡两个阶段可以看出来:JavaScript事件触发的三个阶段分别为:捕获阶段、目标阶段、冒泡阶段而这个阶段就是事件的传递机制。

3.事件代理

现在如果我不想给每个li元素都添加一个点击事件,只给它的父节点ul添加一个点击事件,那么我应该怎么操作呢?

此时,我们可以统一个li元素的父节点ul添加一个点击事件,然后通过ev.target获取到用户当前操作的元素即可,事件触发者一般是事件绑定者的子元素或后代元素。

  1. document.querySelector('ul').addEventListener('click', ev => {
  2. // 事件绑定者
  3. console.log(ev.currentTarget);
  4. // 事件触发者,一般情况下是事件绑定者的子集
  5. console.log(ev.target);
  6. });
  • 效果图:

事件代理

二、利用所学的知识做一个留言板

  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>留言板</title>
  7. <style>
  8. {
  9. margin: 0;
  10. padding: 0;
  11. font-size: 10px;
  12. box-sizing: border-box;
  13. font-family: Helvetica Neue,Helvetica,PingFang SC,Tahoma,Arial,sans-serif;
  14. }
  15. ul, li {
  16. list-style: none;
  17. }
  18. .box {
  19. width: 50rem;
  20. height: 10rem;
  21. margin: 1rem auto;
  22. }
  23. h2 {
  24. font-size: 3rem;
  25. display: flex;
  26. justify-content: center;
  27. margin-bottom: 1rem;
  28. }
  29. #message {
  30. display: flex;
  31. flex-flow: column nowrap;
  32. width: 100%;
  33. }
  34. #message textarea {
  35. width: 100%;
  36. min-height: 10rem;
  37. padding: .6rem 1rem;
  38. border: 1px solid #c9c9c9;
  39. border-radius: 2px;
  40. outline: none;
  41. font-size: 1.4rem;
  42. font-family: inherit;
  43. }
  44. #message .button {
  45. width: 100%;
  46. height: 5rem;
  47. margin-top: 1rem;
  48. }
  49. #message .button .btn {
  50. padding: 0 2rem;
  51. height: 3.8rem;
  52. border-radius: 3px;
  53. border-width: 0;
  54. font-size: 1.2rem;
  55. outline: none;
  56. cursor: pointer;
  57. }
  58. #message .button .btn:hover {
  59. transition: all .3s;
  60. opacity: .8;
  61. }
  62. #message .button .btn.btn-submit {
  63. background-color: #3e7771;
  64. color: #ffffff;
  65. }
  66. #message .button .btn.btn-rest {
  67. margin-left: .5rem;
  68. }
  69. #message .content-wrap em.tips {
  70. font-style: normal;
  71. color: red;
  72. margin-top: 1rem;
  73. }
  74. #list {
  75. width: 100%;
  76. display: grid;
  77. grid-template-columns: 1fr;
  78. grid-template-rows: auto;
  79. margin-top: 1rem;
  80. }
  81. #list li {
  82. display: grid;
  83. grid-template-columns: 4rem 1fr;
  84. grid-template-rows: auto auto;
  85. gap: .5rem;
  86. margin-top: 1rem;
  87. }
  88. #list li.item .avatar {
  89. width: 4rem;
  90. height: 4rem;
  91. }
  92. #list li.item .avatar img {
  93. width: 100%;
  94. height: 100%;
  95. border-radius: 50%;
  96. }
  97. #list li.item .comment {
  98. display: grid;
  99. grid-template-columns: 1fr;
  100. row-gap: .5rem;
  101. }
  102. #list li.item .comment .title {
  103. display: flex;
  104. flex: rows nowrap;
  105. align-items: center;
  106. }
  107. #list li.item .comment .title .nick > * {
  108. font-size: 1.6rem;
  109. color: #000000;
  110. }
  111. #list li.item .comment .title .time {
  112. color: #8c92a4;
  113. margin-left: .5rem;
  114. font-size: 1.2rem;
  115. color: #8c92a4;
  116. font-size: 1.4rem;
  117. }
  118. #list li.item .comment .context{
  119. color: #8c92a4;
  120. font-size: 1.2rem;
  121. color: #40485B;
  122. font-size: 1.4rem;
  123. margin: .5rem 0;
  124. }
  125. #list li.item .del {
  126. margin: .5rem 0;
  127. display: grid;
  128. cursor: pointer;
  129. border: none;
  130. place-self: center end;
  131. grid-area: 2 / 1 / 2 / 3;
  132. padding: .5rem;
  133. outline: none;
  134. }
  135. hr {
  136. border-color: rgba(222, 222, 222, .5);
  137. }
  138. </style>
  139. </head>
  140. <body>
  141. <div class="box">
  142. <h2>留言板</h2>
  143. <div id="message">
  144. <div class="content-wrap">
  145. <textarea class="content" name="message" value="ddd" placeholder="请输入内容"></textarea>
  146. <em class="tips" style="display: none;">* 留言内容不能为空!</em>
  147. </div>
  148. <div class="button">
  149. <button class="btn btn-submit">立即提交</button>
  150. <button type="reset" class="btn btn-rest">重置</input>
  151. </div>
  152. </div>
  153. <hr>
  154. <ul id="list">
  155. </ul>
  156. </div>
  157. <script>
  158. // 获取元素
  159. const msg = document.querySelector('.content');
  160. // 提交按钮
  161. const btn = document.querySelector('.btn-submit');
  162. console.log(btn);
  163. // 重置按钮
  164. const resetBtn = document.querySelector('.btn-rest');
  165. console.log(resetBtn);
  166. // 评论列表
  167. const ul = document.querySelector('#list');
  168. btn.addEventListener('click', ev => {
  169. // 获取文本框的内容:msg.value
  170. let str = ` <li class="item" >
  171. <div class="avatar">
  172. <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAHCklEQVRoQ+2Zd1BUVxTGv106K82VJk1BEHAElGBBYxR1Eomxm6gJjo4tGnUsmZiIRkfDRMeWaOwmxthLNBg10dhFUBEZEASRLkVAWFjaUpbNnOvsm0VBGN9j3MG9f8Huffed3/nuPeWuqPvpaSq8Q0OkA27nausUbucCQ6ewTuF25gHdlm5ngr6Co1NYp3A784BuS7czQbUnaHXpYIcprkGwMJDgiTwX+55cYMYNtvNFTmURUsvz2sT3b21L97P2wnr/WbA1scKj0iyMu7YaS3tMRIjbMKTIc7A85tc2geYNPNZ5IMj41o4ChQybE0+xZ9TApObIy6FY5z8Tnzj1h1gkwkNZRptA8wb+ofd0THAZ1FpephrBNQVMi2hCPy7LwdfRuwRVmjfwSt8vMMZ5AAdsINaHoVgfKqig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tlHt/6jOPRdUdyWeZUdQMhPnPgKK+BvamUgafV1WMxdE7EVeS9orBL0fqpNJsFrmdJNZsblltJX5JDsfBtP94w9ICvBSmBca7vI+ATt3xbcy+RgYt6zkJIa7DoCcWg6KrZiDTnKhZf1NgM9EzYqqyY1Gej3UPjyKiIEEQWEGAm7KEtjo1ETbGlkwhau/O59xt0ujeUndseG82HEw7cd9TirqUF4MNCcdZ17XKbyp8rVxxIO0SwrMjecHzVvjlt1Nrt63vAvS3eXEL8m9uNBbfezV3UiMxuWsQ/KXurBJTj6eVReysnsq6yX2mDmzZlYWYHbmFRfE3HYIDL/Yej2ndPnxRL1cVYWn0bu7sUjCiqDvCoQ/sTTs2mY8zKp5hbtTPHFRPq64sTtAOiC1JxeQbYW/Kyp4TFHhS1yFY0mMCzA1MWY6lQEbl5CjnQNA2dzOz53Kw2mrqhSnHelm4wNPCCfUNShxMv4z1D4+xKXRnFuo7BaZ6xlz5yYdYMGAqEFb6hrCUQsXH30+jWCDTTF1qQykgFSvkCH8ayaIvndPZHh9jnucoUPMhr6tiF33HMq5hU8CXrFKj1BQWd4TV03yGIMAEtabXNAZLMFGFSVhwdxtXT6sbC1I9qSwbZ7Ii2F2V5tA8+9RhUV7OrSqGg6mURe5UeS4+u/ED756YN7CmsgRADTulIM2LdJrjKLHGxdxopmZzQ9NxmnNom+9PvYhNiSf5iCvMGSYYyrlUYFC1FRZ/hFcJSNDf+UxmxQwpTTuGys2Fd7fzVlewoEXV1XT3j1hzIES9S4ZRX0wXenSXpb7N5C2v0FFaCIPaeg3eZ7itDRR6fR2w0B7VtvV0CmubIkLbo1NYaI9q23o6hbVNEaHt0SkstEe1bT2dwtqmiND26BQW2qPatt47p/D/fz1ommzIsNIAAAAASUVORK5CYII=" />
  173. </div>
  174. <div class="comment">
  175. <div class="title">
  176. <span class="nick"><b>残破的蛋蛋</b></span>
  177. <div class="time">${formatDate(Date.now())}</div>
  178. </div>
  179. <div class="context">${msg.value}</div>
  180. <hr>
  181. </div>
  182. <button class="del" onclick="del(this)">删除</button>
  183. </li>
  184. `;
  185. if (msg.value.length === 0) {
  186. console.log(1);
  187. document.getElementsByClassName('tips')[0].style.display = 'block';
  188. return false;
  189. }
  190. ul.insertAdjacentHTML('afterbegin', str);
  191. msg.value = '';
  192. });
  193. // 删除评论
  194. function del(element) {
  195. confirm('确定要删除这条评论吗?') ? element.parentNode.outerHTML = null : false;
  196. }
  197. // 重置内容
  198. resetBtn.onclick = function () {
  199. msg.value = '';
  200. }
  201. // 获取时间
  202. function formatDate(time) {
  203. let date = new Date(time);
  204. console.log(date);
  205. let y = date.getFullYear();
  206. console.log(y);
  207. let m = date.getMonth();
  208. m = date.getMonth()+1 < 10 ? `0${date.getMonth()+1}` : date.getMonth()+1
  209. console.log(m);
  210. let d = date.getDate();
  211. console.log(d);
  212. let h = date.getHours();
  213. console.log(h);
  214. let i = date.getMinutes();
  215. console.log(i);
  216. let s = date.getSeconds();
  217. console.log(s);
  218. return `${y}-${m}-${d} ${h}-${i}-${s}`;
  219. }
  220. </script>
  221. </body>
  222. </html>
  • 效果图:

留言板

三、字符串常用的10个方法

  1. // 1. concat() 拼接字符串
  2. let str = "html".concat(" css ", "php !", 888);
  3. console.log(str); // html css php !888
  4. // 2. slice(start, end) 取子串
  5. str = "html css php !";
  6. let res = str.slice(0, 5);
  7. console.log(res); // html
  8. // 省略第二个参数,表示从0一直取到末尾
  9. res = str.slice(0);
  10. console.log(res); // html css php !
  11. res = str.slice(5);
  12. console.log(res); // html css php !
  13. res = str.slice(-5);
  14. console.log(res); // html css php !
  15. res = str.slice(-5, -2);
  16. console.log(res); // html css php !
  17. // 3.substr(start, length)
  18. res = str.substr(0, 6);
  19. console.log(res);
  20. res = str.substr(-5, 3);
  21. console.log(res);
  22. // 4.trim():删除两端空格
  23. let pwd = " root888 ";
  24. console.log(pwd.length); // 13
  25. // 5.将字符串打成数组
  26. res = str.split("");
  27. console.log(res);
  28. // 比如说从邮箱中解析出用户和邮箱地址
  29. res = "admin@admin.cn".split("@");
  30. console.log(res[0]); // admin
  31. console.log(res[1]); // admin.cn
  32. // 6.查找字符串中的字符串
  33. str = "The full name of China is the People's Republic of China.";
  34. // 使用indexof()方法返回字符串指定的文本首次出现的位置
  35. console.log(str.indexOf('China')); // 17
  36. // lastIndexOf()方法返回指定的字符串在文本中最后一次出现的位置
  37. console.log(str.lastIndexOf('the')); // 26
  38. // indexof()和lastIndexOf()如果在文中本找到指定的字符串,则返回-1
  39. console.log(str.indexOf('hello')); // -1
  40. console.log(str.lastIndexOf('hello')); // -1
  41. // 7.search()方法返回指定文本在文中第一次出现的位置
  42. str = "The full name of China is the People's Republic of China.";
  43. console.log(str.search('China')); // 17
  44. // 8.替换字符串
  45. str = "Please visit microsoft and Microsoft!";
  46. console.log(str.replace('Microsoft', '微软')); // Please visit microsoft and 微软!
  47. // 默认的replace()只替换首次匹配的字符串,并且对大小写敏感
  48. // 9.大小写转换
  49. str = 'Hello World!';
  50. // 字符串转大写
  51. console.log(str.toUpperCase()); // HELLO WORLD!
  52. console.log(str.toLocaleUpperCase()); // HELLO WORLD!
  53. // 字符串转小写
  54. console.log(str.toLowerCase()); // hello world!
  55. console.log(str.toLocaleLowerCase()); // hello world!
  56. // 10.charAt()方法返回字符串中指定下标的字符串
  57. str = 'Hello World';
  58. console.log(str[0]); // H
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