博主信息
博文 5
粉丝 0
评论 0
访问量 4177
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
classList对象、blur事件进行表单非空验证
幽幽小帆
原创
463人浏览过

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <style>
  9. body {
  10. background-color: mediumturquoise;
  11. color: #444;
  12. font-weight: lighter;
  13. }
  14. #login {
  15. background-color: azure;
  16. width: 20em;
  17. border-radius: 0.3em;
  18. box-shadow: 0 0 1em #888;
  19. box-sizing: border-box;
  20. padding: 1em 2em 1em;
  21. margin: 5em auto;
  22. display: grid;
  23. grid-template-columns: 3em 1fr;
  24. gap: 1em 0;
  25. }
  26. .btn1 {
  27. color: red;
  28. }
  29. .btn2 {
  30. background-color:lightgreen;
  31. }
  32. .btn3 {
  33. background-color: blue;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <form action="" method="post" id="login" >
  39. <label for="email">邮箱:</label>
  40. <input type="email" id="email" name="email" value="" autofocus>
  41. <label for="password">密码:</label>
  42. <input type="password" name="password" id="password">
  43. <button name="submit" class="btn">登录</button>
  44. </form>
  45. <script>
  46. // 1. 实例演示classList对象
  47. let btn = document.querySelector('.btn');
  48. btn.classList.add('btn1'); // 剩下:btn btn1
  49. console.log(btn.classList.contains('btn'));
  50. btn.classList.add('btn2'); // 剩下:btn btn1 btn2
  51. btn.classList.remove('btn'); // 剩下:btn1 btn2
  52. btn.classList.replace('btn2', 'btn3'); // 剩下:btn1 btn3
  53. btn.classList.toggle('btn3'); // 剩下:btn1
  54. // 2. 使用blur事件进行表单非空验证
  55. document.forms.login.email.onblur = function() {
  56. if (this.value.length === 0){
  57. alert("邮箱不能为空");
  58. return false;
  59. }
  60. };
  61. document.forms.login.password.onblur = function(){
  62. if(this.value.length === 0){
  63. alert("密码不能为空");
  64. return false;
  65. }
  66. };
  67. </script>
  68. </body>
  69. </html>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学