Blogger Information
Blog 11
fans 0
comment 0
visits 6376
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js-基础(五)classList对象、blur事件进行表单非空验证
Technology has temperature
Original
695 people have browsed it

1.classList对象

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

2.blur事件进行表单非空验证

  1. function check(element) {
  2. //禁用默认行为
  3. event.preventDefault();
  4. //阻止冒泡
  5. event.stopPropagation();
  6. let email = element.form.email;
  7. let password = element.form.password;
  8. if (email.value.length === 0) {
  9. alert("邮箱为空");
  10. email.focus();
  11. return false;
  12. } else if (password.value.length === 0) {
  13. alert("密码为空");
  14. password.focus();
  15. return false;
  16. } else {
  17. return true;
  18. }
  19. }
  20. //blur失去焦点时触发
  21. document.forms.login.email.onblur = function () {
  22. if (this.value.length === 0) {
  23. alert("邮箱不能为空");
  24. return false;
  25. }
  26. };
  27. document.forms.login.password.onblur = function () {
  28. if (this.value.length === 0) {
  29. alert("密码不能为空");
  30. return false;
  31. }
  32. };
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