博主信息
博文 18
粉丝 0
评论 2
访问量 12327
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
实例演示classList对象和blur事件
弦知音的博客
原创
679人浏览过

实例演示classList对象和blur事件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>实例演示classList对象和blur事件</title>
  8. </head>
  9. <style>
  10. .bgc {
  11. background-color: blue;
  12. }
  13. .bgc2 {
  14. background-color: yellow;
  15. }
  16. .active {
  17. color: red;
  18. }
  19. .boder {
  20. border: 1px solid black;
  21. }
  22. .title {
  23. font-size: 60px;
  24. }
  25. </style>
  26. <body>
  27. <h2 class="title">Hello World</h2>
  28. <script>
  29. const h2 = document.querySelector('.title');
  30. // 添加
  31. h2.classList.add('active');
  32. h2.classList.add('bgc');
  33. // 判断
  34. console.log(h2.classList.contains('hello') ? 'hello world' : 'hello php');
  35. // 移除
  36. h2.classList.remove('bgc');
  37. // 替换
  38. h2.classList.replace('active', 'bgc2');
  39. // 动态切换
  40. h2.classList.toggle('boder');
  41. </script>
  42. <!-- 表单非空验证 -->
  43. <form action="" method="post" id="login">
  44. <label for="email">邮箱:</label>
  45. <input type="email" id="email" name="email" value="" autofocus />
  46. <label for="password">密码:</label>
  47. <input type="password" id="password" name="password" />
  48. <!-- 1. type="button" 来表示这是一个普通按钮,没有提交行为 -->
  49. <button type="button" name="submit" onclick="check(this)">登录</button>
  50. </form>
  51. <!-- 表单非空验证 -->
  52. <script>
  53. //点击button触发check函数进行验证
  54. function check(ele) {
  55. // 禁用默认行为
  56. event.preventDefault();
  57. // 防止冒泡
  58. event.stopPropagation();
  59. // 非空验证
  60. // 每一个表单控件input,都有一个form属性,指向所属的表单元素
  61. // 通过form属性可以获取到表单中所有元素的值
  62. let email = ele.form.email;
  63. let password = ele.form.password;
  64. if (email.value.length === 0) {
  65. email.setAttribute('placeholder', '邮箱不能为空,请输入');
  66. email.focus();
  67. return false;
  68. } else if (password.value.length === 0) {
  69. password.setAttribute('placeholder', '密码不能为空,请输入');
  70. password.focus();
  71. return false;
  72. }
  73. }
  74. // 失去焦点验证;
  75. document.forms.login.email.onblur = function () {
  76. if (this.value.length === 0) {
  77. this.setAttribute('placeholder', '不能为空,请输入');
  78. return false;
  79. }
  80. };
  81. </script>
  82. </body>
  83. </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+教程免费学