Blogger Information
Blog 22
fans 0
comment 0
visits 11444
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演示classList和表单非空验证
没耐心的渔翁
Original
369 people have browsed it

classList

  • classList.add()添加
    h3.classList.add('active');
  • classList.remove() 移除样式
    h3.classList.remove('bgc');
  • classList.replace()替换*里面有两个值 第一个是原来的,第二个是将要替换的
    h3.classList.replace('active','aaa')
  • toggle()动态切换如果之前有这个样式 就去掉,没有择加上
    h3.classList.toggle('bgc2');

    表单非空验证

    1. function chen(aa) {
    2. console.log(aa);
    3. //去掉submit提交方法
    4. event.preventDefault();
    5. //阻止冒泡
    6. event.stopPropagation();
    7. //每个ipnt都有form属性
    8. console.log(aa.form);
    9. // //拿到form中的用户名
    10. console.log(aa.form.username);
    11. let name = aa.form.username;
    12. // //拿到form中的密码
    13. console.log(aa.form.password);
    14. let password = aa.form.password;
    15. //判断非空
    16. if (name.value.length === 0) {
    17. alert('用户名不能为空');
    18. return false;
    19. } else if (password.value.length === 0) {
    20. alert('请输入密码');
    21. return false;
    22. } else {
    23. alert('登录成功');
    24. }
    25. }
    26. //blur失去焦点触发
    27. // const la = document.forms.log.username;
    28. // console.log(la);
    29. document.forms.log.username.onblur = function () {
    30. if(this.value.length === 0){
    31. alert('请输入用户名');
    32. return false;
    33. }else if(document.forms.log.password.onblur=function(){
    34. if(this.value.length === 0){
    35. //alert('请输入密码');
    36. return false;
    37. }
    38. }){
    39. }
    40. }
    41. //input,值发改变就触发,不必等失去焦点
    42. document.forms.log.password.oninput = function () {
    43. console.log("123");
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