Blogger Information
Blog 33
fans 0
comment 0
visits 17120
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
classList 对象实例演示,使用 blur 进行非空验证
lucaslwk
Original
395 people have browsed it

classList 对象实例演示,使用 blur 进行非空验证

一.classList 对象实例演示

  1. const box = document.querySelector(".box");
  2. //parseInt()字符串转换为数字,window.getComputedStyle()全局方法获取计算样式
  3. let borderWidth = parseInt(window.getComputedStyle(box).borderWidth) + 5;
  4. box.style.borderWidth = borderWidth + "px";
  5. //添加
  6. box.classList.add("backgroundColor");
  7. //判断
  8. console.log(box.classList.contains("backgroundColor"));
  9. //移除
  10. box.classList.remove("backgroundColor");
  11. //替换('旧值','新值')
  12. box.classList.replace("box", "borderChange");
  13. //切换,类名有则移除,没有则添加
  14. box.classList.toggle("colorChange");
  15. box.classList.toggle("borderChange");

二.使用 blur 进行非空验证

非空验证

  1. function check(element) {
  2. //禁用默认行为
  3. event.preventDefault();
  4. //阻止冒泡
  5. event.stopPropagation();
  6. console.log(element.form);
  7. let email = element.form.email;
  8. let password = element.form.password;
  9. if (email.value.length === 0) {
  10. alert("邮箱为空");
  11. email.focus();
  12. return false;
  13. } else if (password.value.length === 0) {
  14. alert("密码为空");
  15. password.focus();
  16. return false;
  17. } else {
  18. return true;
  19. }
  20. }
  21. //focus获取焦点时触发
  22. document.forms.login.email.onfocus = function () {
  23. email.value.length === 0
  24. ? false
  25. : console.log(document.forms.login.email.value);
  26. };
  27. document.forms.login.password.onfocus = function () {
  28. password.value.length === 0
  29. ? false
  30. : console.log(document.forms.login.password.value);
  31. };
  32. //blur失去焦点时触发
  33. document.forms.login.email.onblur = function () {
  34. if (this.value.length === 0) {
  35. alert("邮箱不能为空");
  36. return false;
  37. }
  38. };
  39. document.forms.login.password.onblur = function () {
  40. if (this.value.length === 0) {
  41. alert("密码不能为空");
  42. return false;
  43. }
  44. };
  45. //change值发生改变且失去焦点时触发
  46. document.forms.login.password.onchange = function () {
  47. console.log("密码已改变");
  48. };
  49. //input值发生改变时触发
  50. document.forms.login.password.oninput = function () {
  51. console.log(this.value);
  52. };
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