Blogger Information
Blog 33
fans 0
comment 0
visits 18749
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js操作class和使用blur事件进行表单非空验证
李玉峰
Original
590 people have browsed it

1.js操作class

  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>js操作class</title>
  8. <style>
  9. .title {
  10. border: 1px solid blue;
  11. }
  12. .fontColor {
  13. color: orangered;
  14. }
  15. .bgc {
  16. background-color: cyan;
  17. }
  18. .bgc2 {
  19. background-color: wheat;
  20. }
  21. .fontColor2 {
  22. color: blueviolet;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <h2 class="title">用js操作class</h2>
  28. <script>
  29. const h2 = document.querySelector(".title");
  30. // 方法:classList
  31. // 添加:add()
  32. // 修改字体颜色和背景
  33. // h2.classList.add("fontColor");
  34. // h2.classList.add("bgc");
  35. h2.classList.add("fontColor", "bgc");
  36. // 判断:contains(),是否具有”active“属性:如果有返回:true,没有返回:false
  37. console.log(h2.classList.contains("fontColor"));
  38. console.log(h2.classList.contains("bgc2"));
  39. // 移除:romove()
  40. // 移除背景色
  41. h2.classList.remove("bgc");
  42. // 替换:replace()
  43. // 把"fontColor"替换成 "fontColor2"
  44. h2.classList.replace("fontColor", "fontColor2");
  45. // 动态切换class:toggle()
  46. // 如果之前没有’bgc2‘这个样式就加上,如果有就取消
  47. // h2.classList.add("bgc2");
  48. h2.classList.toggle("bgc2");
  49. </script>
  50. </body>
  51. </html>

2.使用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. <link rel="stylesheet" href="style.css" />
  9. </head>
  10. <body>
  11. <!-- 2.禁用表单提交行为: onsubmit="return false;" -->
  12. <form action="" method="post" id="login">
  13. <label class="title">用户登录</label>
  14. <label for="email">邮箱:</label>
  15. <input type="email" id="email" name="email" value="" autofocus />
  16. <label for="password">密码:</label>
  17. <input type="password" id="password" name="password" />
  18. <!-- 1.禁用表单提交行为: type="button" 来表示这是一个普通按钮,没有提交行为 -->
  19. <button name="submit" onclick="check(this)">登录</button>
  20. </form>
  21. <script>
  22. function check(ele) {
  23. // console.log(ele);
  24. // 3.(推荐动作1)禁用默认表单提交行为:preventDefault
  25. event.preventDefault();
  26. // (推荐动作2)防止冒泡
  27. event.stopPropagation();
  28. // (推荐动作3)非空验证
  29. // 每一个表单控件input, 都有一个form属性;
  30. console.log(ele.form);
  31. // 通过form属性可以获取到表单中所有元素的值;
  32. // console.log(ele.form.email);
  33. let email = ele.form.email;
  34. let password = ele.form.password;
  35. // if (email.value.length === 0) {
  36. // alert("邮箱不能为空,请输入");
  37. // email.focus();
  38. // return false;
  39. // } else if (password.value.length === 0) {
  40. // alert("密码不能为空,请输入");
  41. // email.focus();
  42. // return false;
  43. // } else {
  44. // alert("验证通过,正在提交");
  45. // }
  46. }
  47. // 失去焦点时触发
  48. document.forms.login.email.onblur = function () {
  49. if (this.value.length === 0) {
  50. alert("邮箱不能为空");
  51. return false;
  52. }
  53. };
  54. document.forms.login.password.onblur = function () {
  55. if (this.value.length === 0) {
  56. alert("密码不能为空");
  57. return false;
  58. }
  59. };
  60. //change值发生改变且失去焦点时触发
  61. document.forms.login.email.onchange = function () {
  62. console.log("邮箱已改变");
  63. };
  64. //input值发生改变时触发
  65. document.forms.login.password.oninput = function () {
  66. console.log(this.value);
  67. };
  68. </script>
  69. </body>
  70. </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