Blogger Information
Blog 29
fans 0
comment 0
visits 14891
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js--用blur事件做表单验证,常用字符串、数组api小结
cool442
Original
488 people have browsed it

1. 用blur事件做表单验证

  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>表单事件</title>
  8. </head>
  9. <body>
  10. <style>
  11. html {
  12. font-size: 100px;
  13. }
  14. * {
  15. padding: 0;
  16. margin: 0;
  17. border: none;
  18. box-sizing: border-box;
  19. }
  20. body {
  21. font-size: 16px;
  22. background-color: rgb(51, 169, 204);
  23. }
  24. #login {
  25. width: 3.5rem;
  26. margin: 2rem auto;
  27. padding: 0.15rem 0.25rem;
  28. background-color: rgb(128, 206, 227);
  29. display: grid;
  30. grid-template-columns: 0.6rem 1fr;
  31. gap: 0.15rem 0;
  32. }
  33. h3 {
  34. margin-bottom: 0.2rem;
  35. text-align: center;
  36. grid-area: auto / span 2;
  37. }
  38. button {
  39. padding: 0.05rem;
  40. color: white;
  41. background-color: rgb(62, 141, 186);
  42. margin-bottom: 0.2rem;
  43. grid-area: auto / 2 / auto;
  44. }
  45. button:hover {
  46. cursor: pointer;
  47. background-color: rgb(38, 105, 138);
  48. }
  49. </style>
  50. <form id="login">
  51. <h3>用户登录</h3>
  52. <label for="email">邮箱:</label>
  53. <input type="email" name="email" id="email" onblur="checkEmail(this)" />
  54. <label for="pass">密码:</label>
  55. <input type="password" name="pass" id="pass" onblur="checkPass()" />
  56. <button onclick="login()">登录</button>
  57. </form>
  58. <script>
  59. let checkEmailState = false;
  60. let checkPassState = false;
  61. // 邮箱输入框失去焦点时检查输入
  62. function checkEmail(ev) {
  63. // 阻止冒泡
  64. event.stopPropagation();
  65. let email = ev.form.email.value;
  66. if (email.length === 0) {
  67. alert("邮箱名不能为空!");
  68. checkEmailState = false;
  69. } else {
  70. checkEmailState = true;
  71. }
  72. }
  73. // 密码失去焦点时检查输入
  74. function checkPass() {
  75. event.stopPropagation();
  76. let pass = event.target;
  77. if (pass.value.length === 0) {
  78. alert("密码不能为空!");
  79. checkPassState = false;
  80. } else {
  81. checkPassState = true;
  82. }
  83. }
  84. function login() {
  85. if (checkEmailState && checkPassState) {
  86. alert("验证通过!");
  87. } else {
  88. alert("验证错误,请重新输入!");
  89. }
  90. }
  91. </script>
  92. </body>
  93. </html>

2.常用字符串api

  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>字符常用api</title>
  8. </head>
  9. <body>
  10. <script>
  11. let msg = "php中文网:公益在线php培训,帮助PHP学习者快速成长!";
  12. // 1. length:字符串长度
  13. console.log(msg.length);
  14. // 2. charAt:索引,第几个字符
  15. console.log(msg.charAt(8));
  16. // 字符串可当作数组
  17. console.log(msg[8]);
  18. // 3. indexOf:字符索引,返回是第几个字符
  19. console.log(msg.indexOf("学"));
  20. // 4. search:同上,支持正则表达
  21. console.log(msg.search("学"));
  22. // 5. replace:替换
  23. console.log(msg.replace("中文网", ".cn中文网"));
  24. // 6. slice:取子串,开始,结束
  25. console.log(msg.slice(3, 6));
  26. // 7. substr:取子串,开始,长度
  27. console.log(msg.substr(3, 6));
  28. // 负数为从倒数第几个开始
  29. console.log(msg.substr(-6, 3));
  30. // 8. split:转为数组,每个字符为一个值
  31. let msgArr = msg.split("");
  32. console.log(msgArr);
  33. console.log(msgArr[3]);
  34. // 指定转换数量,例:把6个字符转为数组
  35. console.log(msg.split("", 6));
  36. // 9. toLowerCase,toUpperCase:大小写转换
  37. console.log(msg.toLowerCase());
  38. console.log(msg.toUpperCase());
  39. // 10. 与html相关
  40. console.log(`<a href="https://php.cn">${msg}</a>`);
  41. // link实现上面连接
  42. console.log(msg.link("https://php.cn"));
  43. // 添加加粗元素
  44. console.log(msg.bold());
  45. // 创建锚点
  46. console.log(msg.anchor("url"));
  47. </script>
  48. </body>
  49. </html>

3. 数组常用api

  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>数组常用api</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 一、创建数组的方式
  12. // 1. 用字面量,数组里支持任何类型数据
  13. let arr = [
  14. 1,
  15. 3,
  16. 6,
  17. "中文网",
  18. true,
  19. { i: 55, j: 88, aa() {} },
  20. () => "这是函数。",
  21. ];
  22. console.log(arr);
  23. // 2. 用...rest创建
  24. let arrOne = [...arr];
  25. console.log(arrOne);
  26. // 扩展数组
  27. let arrTwo = [56, 78, "ok", ...arrOne];
  28. console.log(arrTwo);
  29. // 3. Array.of创建
  30. let arrThree = Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  31. console.log(arrThree);
  32. arrThree = [1, 3, 5, 7, 9];
  33. let arrFour = Array.of(...arrThree);
  34. console.log(arrFour);
  35. // 4. Array.from 创建类数组
  36. // 定义类
  37. const weather = {
  38. 0: "暴风雨",
  39. 1: "晴",
  40. 2: "小雨",
  41. length: 3,
  42. };
  43. // weather为类
  44. console.log(weather);
  45. // 转为数组
  46. console.log(Array.from(weather));
  47. // 二、数组增删
  48. let arrEr = [5, 8, 10];
  49. // 从尾部增加
  50. arrEr.push(15);
  51. arrEr.push(25, 28);
  52. console.log(arrEr);
  53. // 从尾部删除
  54. arrEr.pop();
  55. arrEr.pop();
  56. arrEr.pop();
  57. console.log(arrEr);
  58. // 从头部增加
  59. arrEr.unshift(4);
  60. arrEr.unshift(1, 2);
  61. console.log(arrEr);
  62. // 从头删除
  63. arrEr.shift();
  64. arrEr.shift();
  65. arrEr.shift();
  66. console.log(arrEr);
  67. // 按索引删除,只能删除值,长度不变
  68. delete arrEr[1];
  69. console.log(arrEr);
  70. console.log(arrEr.length);
  71. // 通过修改数组长度,增加空值
  72. arrEr.length = 5;
  73. console.log(arrEr);
  74. // 三、迭代方法和遍历元素
  75. // 1.forEach
  76. let arrFor = [10, 20, 30, 40, 50, 60, 70, 80, 90];
  77. // forEach的参数是回调函数
  78. // 回调函数的参数:
  79. // 第一个:从数组中取的值
  80. // 第二个:数组的索引
  81. // 第三个:要遍历的数组
  82. // 没有返回值,不能赋值给变量
  83. arrFor.forEach(function (value, index, arrFor) {
  84. console.log(value, index);
  85. document.body.append(`数组值${value}--`);
  86. });
  87. // 2. map:参数功能与forEach一样,有返回值
  88. let value = arrFor.map((value) => value);
  89. console.log(value);
  90. // 3. every,some:断言函数,返回true,false
  91. // every:判断数组成员是否全部满足条件
  92. console.log(value.every((i) => i > 55));
  93. console.log(value.every((i) => i < 500));
  94. // some:数组成员中只要有1个满足条件就为true
  95. console.log(value.some((i) => i > 55));
  96. console.log(value.some((i) => i < 80));
  97. // 4. filter:返回数组中满足条件的值,组成新数组
  98. let min = value.filter((i) => i < 80);
  99. console.log(min);
  100. // 5. reduce:归并,参数a为累加器,结果放到a,a可设初始值
  101. value = arrFor.reduce(function (a, b, c, d) {
  102. console.log(`${a}--${b}--${c}--${d}`);
  103. return a + b;
  104. }, 100);
  105. // 四、
  106. // 1. sort:排序,
  107. arr = [5, 15, 35, 85];
  108. // 默认按字母表顺序
  109. console.log(arr.sort());
  110. // 按大到小排,要用参数
  111. console.log(arr.sort((a, b) => a - b));
  112. // 从小到大排;
  113. console.log(arr.sort((a, b) => b - a));
  114. // 2. join:连接数组
  115. arr = ["大号", "中号", "小号"];
  116. console.log(arr.join());
  117. console.log(arr.join("--"));
  118. // 3. slice:取子元素
  119. arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
  120. // 第2个到第5个
  121. console.log(arr.slice(2, 5));
  122. // 第2个开始到最后
  123. console.log(arr.slice(2));
  124. // 倒数第6个到倒数第2个
  125. console.log(arr.slice(-6, -2));
  126. // 4. splice:删除、新增、替换
  127. // 从索引1开始删除3个元素
  128. console.log(arr.splice(1, 3));
  129. console.log(arr);
  130. // 替换:从索引1开始替换3个元素
  131. console.log(arr.splice(1, 3, "a", "b", "c"));
  132. console.log(arr);
  133. // 插入:从索引1开始插入3个元素
  134. console.log(arr.splice(1, 0, "红", "黄", "蓝"));
  135. console.log(arr);
  136. let data = ["太阳", "月亮"];
  137. console.log(arr.splice(5, 0, ...data));
  138. console.log(arr);
  139. </script>
  140. </body>
  141. </html>
Correcting teacher:PHPzPHPz

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