Blogger Information
Blog 30
fans 0
comment 0
visits 13900
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
classList对象与用blur事件进行表单非空验证
天宁
Original
418 people have browsed it

class:用js控制

  1. <h2 class="title" id="one">php中文网</h2>
传统方式获取class
  1. const h2 = document.querySelector('.title');
  2. console.log(h2.id);//获取id
  3. console.log(h2.className);//获取class
class添加:add()
  1. h2.classList.add('active');
  2. h2.classList.add('bgc');
  3. //这样之后class的类就添加了两个变成了;
  4. //<h2 class="title active bgc" id="one">php中文网</h2>
class判断:contains()
  1. //判断是否有这个类,存在则返回true,不存在则返回false
  2. console.log(h2.classList.contains('active'));
  3. console.log(h2.classList.contains('hello'));
class移除:remove()
  1. h2.classList.remove('bgc');
class替换:replace()
  1. //replace(要被替换的,新的值)
  2. h2.classList.replace('active', 'em');
动态切换class:toggle()
  1. //如果之前有这个样式,就去掉,没有就加上,互逆的操作
  2. h2.classList.toggle('bgc2');

使用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. // 第3种方式,禁用默认行为
  24. event.preventDefault();
  25. // 防止冒泡
  26. event.stopPropagation();
  27. // 非空验证
  28. // 每一个表单控件input,都有一个form属性,指向所属的表单元素
  29. console.log(ele.form);
  30. // 通过form属性可以获取到表单中所有元素的值
  31. // console.log(ele.form.email);
  32. let email = ele.form.email;
  33. let password = ele.form.password;
  34. if (email.value.length === 0) {
  35. alert('邮箱不能为空');
  36. email.focus();
  37. return false;
  38. } else if (password.value.length === 0) {
  39. alert('密码也不能为空');
  40. email.focus();
  41. return false;
  42. } else {
  43. alert('验证通过');
  44. }
  45. }
  46. //当邮箱输入框失去焦点时,则检测输入框是否为空
  47. document.forms.login.email.onblur = function () {
  48. if (this.value.length === 0) {
  49. alert('邮箱不能为空');
  50. return false;
  51. }
  52. };
  53. //当密码输入失去焦点,则检测密码输入框是否为空
  54. document.forms.login.password.onblur = function () {
  55. if (this.value.length === 0) {
  56. alert('密码不能为空');
  57. return false;
  58. }
  59. };
  60. </script>
  61. </body>
  62. </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