Blogger Information
Blog 14
fans 0
comment 0
visits 8282
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Js表单事件与数组常用 API
#三生
Original
451 people have browsed it

1. 表单事件案例,

使用blur失去焦点进行非空验证
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <style>
  9. body {
  10. background-color: mediumturquoise;
  11. color: #444;
  12. font-weight: lighter;
  13. }
  14. #login {
  15. width: 20em;
  16. border-radius: 0.3em;
  17. box-shadow: 0 0 1em #888;
  18. box-sizing: border-box;
  19. padding: 1em 2em 1em;
  20. margin: 5em auto;
  21. background-color: paleturquoise;
  22. display: grid;
  23. grid-template-columns: 3em 1fr;
  24. gap: 1em 0;
  25. }
  26. #login .title {
  27. grid-area: auto / span 2;
  28. place-self: center;
  29. }
  30. #login input {
  31. border-radius: 0.3em;
  32. border: none;
  33. padding-left: 0.3em;
  34. }
  35. #login input:focus {
  36. outline: none;
  37. box-shadow: 0 0 5px seagreen;
  38. background-color: hsl(283, 100%, 95%);
  39. border-radius: 0.2em;
  40. transition: 0.5s;
  41. }
  42. #login button {
  43. grid-area: auto / 2 / auto;
  44. outline: none;
  45. background-color: lightseagreen;
  46. border: none;
  47. height: 2em;
  48. color: #fff;
  49. }
  50. #login button:hover {
  51. cursor: pointer;
  52. background-color: seagreen;
  53. transition: 0.5s;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <form action="" method="post" id="login">
  59. <label class="title">用户登录</label>
  60. <label for="email">邮箱:</label>
  61. <input type="email" onblur="check(this)" id="email" name="email" value="" autofocus placeholder="请输入邮箱" />
  62. <label for="password">密码:</label>
  63. <input type="password" onblur="check(this)" id="password" name="password" placeholder="请输入密码" />
  64. <button name="submit">登录</button>
  65. </form>
  66. <script>
  67. function check(ele) {
  68. // 1. 防止默认的提交行为, 用户自定义
  69. event.preventDefault();
  70. // 2. 防止冒泡
  71. event.stopPropagation();
  72. // 3. 非空验证
  73. // 每一个控件input,button 都有一个属性form ,和当前表单绑定
  74. let email = ele.form.email;
  75. let password = ele.form.password;
  76. if (email.value.length === 0) {
  77. alert("邮箱不能为空");
  78. email.focus();
  79. return false;
  80. } else if (password.value.length === 0) {
  81. alert("密码不能为空");
  82. password.focus();
  83. return false;
  84. } else {
  85. alert("验证通过");
  86. }
  87. }
  88. </script>
  89. </body>
  90. </html>

2. 字符串中常用的api

字符串 说明 let str = “php中文网”;
length 长度 str.length
charAt 索引->元素 str.charAt(3)
indexOf 字符->索引 str.indexOf(“中”)
search indexOf一样,但支持正则等 str.search(“文”)
concat 字符串拼装 str.concat(“(“, “php.cn”, “)”)
replace 替换 str.replace(“中文网”, “.cn”)
slice 子串, 忽略结束索引的值 str.slice(0, 3)
substr 子串, 只需要知道取多少个 str.substr(0, 3)
split 分割字符串 str.split(“”, 3)
toLowerCase 将字符串全部转成小写 str.toLowerCase()
toUpperCase 将字符串全部转成大写 str.toUpperCase()

3.数组常用api

  • 原始数据有可能来自一个外部请求api或文件,而这个原始数据就是数组
  1. let a = [1, 2, 3, 4, 5, 6];
  2. // 经过一些其它操作,例如过滤,再重新生成
  3. arr = Array.of(...a);
  4. console.log(arr);
  1. arr = [1, 2, 3, 4, 5];
  2. delete arr[1];
  3. console.log(arr);//[1, , 3, 4, 5];
  4. console.log(arr.length);//5
  5. arr.length = 4;
  6. console.log(arr);//[1, , 3, 4]
  1. // 1. sort
  2. let arr = [1, 10, 20, 6];
  3. console.log(arr.sort());
  4. // asc
  5. console.log(arr.sort((a, b) => a - b));
  6. // desc
  7. console.log(arr.sort((a, b) => b - a));
  8. // 2. join: array -> string
  9. arr = ["red", "green", "blue"];
  10. console.log(arr.join());
  11. console.log(arr.join("-"));
  12. // 3. slice: 子元素
  13. arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  14. console.log(arr.slice(2, 5));
  15. console.log(arr.slice(2));
  16. console.log(arr.slice(-6, -3));
  17. // 4. splce: 删除, 新增, 替换
  18. // delete
  19. console.log(arr);
  20. console.log(arr.splice(1, 2));
  21. console.log(arr);
  22. // update
  23. console.log(arr.splice(1, 2, "a", "b"));
  24. console.log(arr);
  25. // insert
  26. console.log(arr.splice(1, 0, "red", "green"));
  27. console.log(arr);
  28. let data = ["red", "green", "blue"];
  29. console.log(arr.splice(1, 0, ...data));
  30. console.log(arr);
  31. // 数组反转, 数组拼装, 拉平与一维数组... mdn
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