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>

更多相关文章

  1. classlist对象和表单非空验证
  2. 1. 实例演示classList对象 2. 使用blur事件进行表单非空验证
  3. 表单、iframe后台、元素样式来源优先级
  4. classList对象与用blur事件进行表单非空验证
  5. js操作class和使用blur事件进行表单非空验证
  6. ClassList对象学习总结、表单事件非空验证
  7. javascript-基础(五)
  8. classList对象、blur事件进行表单非空验证
  9. 实例演示classList对象及表单非空验证

随机推荐

  1. 如何将占位符放在struts textfield标记中
  2. 使用Acrobat的javascript调试器
  3. PHP发布请求未识别的索引错误
  4. 为什么elementFromPoint()在IE 11上的ifram
  5. JavaScript - 检测click事件是否触发了D
  6. WebKit "拒绝设置不安全的标题'内容长度'
  7. 关于JavaScript变量声明及其初始化的问题
  8. 原生js操作dom的笔记整理
  9. Javascript操作DOM常用API总结
  10. Angularjs指令ng-hide在单元格模板中不对