博客列表 >ClassList对象、表单事件

ClassList对象、表单事件

shooter
shooter原创
2022年04月07日 15:38:25602浏览

ClassList对象

效果图

代码

  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>class: 用js控制</title>
  8. <style>
  9. .title {
  10. border: 1px solid black;
  11. background-color: aquamarine;
  12. color: red;
  13. }
  14. .active {
  15. color: blueviolet;
  16. }
  17. .bgc {
  18. background-color: chartreuse;
  19. }
  20. .em {
  21. color: darkolivegreen;
  22. }
  23. .bgc2 {
  24. background-color: antiquewhite;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h2 class="title">hello</h2>
  30. <script>
  31. const h2 = document.querySelector('.title');
  32. //add()添加
  33. h2.classList.add('active');
  34. h2.classList.add('bgc');
  35. //contains()判断
  36. console.log(h2.classList.contains('active'));
  37. console.log(h2.classList.contains('你好'));
  38. //remove()移除
  39. h2.classList.remove('bgc');
  40. //replace()替换
  41. h2.classList.replace('active', 'em');
  42. //toggle(): 动态切换class
  43. //如果之前没这个样式,就加上,如果有,就去掉
  44. h2.classList.toggle('bgc2');
  45. </script>
  46. </body>
  47. </html>

表单事件

效果图

代码

  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. <link rel="stylesheet" href="css/2.css" />
  9. </head>
  10. <body>
  11. <form action="" method="post" id="login">
  12. <label class="title">用户登录</label>
  13. <label for="email">邮箱:</label>
  14. <input type="email" id="email" name="email" value="" autofocus />
  15. <label for="password">密码:</label>
  16. <input type="password" id="password" name="password" />
  17. <button name="submit" onclick="check(this)">登录</button>
  18. </form>
  19. <script>
  20. function check(ele) {
  21. //禁用默认行为
  22. event.preventDefault();
  23. //防止冒泡
  24. event.stopPropagation();
  25. //非空验证
  26. // 每一个表单控件input,都有一个form属性,指向所属的表单元素
  27. console.log(ele.form);
  28. let email = ele.form.email;
  29. let password = ele.form.password;
  30. if (email.value.length === 0) {
  31. alert('邮箱不能为空');
  32. email.focus();
  33. return false;
  34. } else if (password.value.length === 0) {
  35. alert('密码不能为空');
  36. email.focus();
  37. return false;
  38. } else {
  39. alert('验证成功');
  40. }
  41. }
  42. document.forms.login.email.onblur = function () {
  43. if (this.value.length === 0) {
  44. alert('邮箱不能为空');
  45. return false;
  46. }
  47. };
  48. document.forms.login.password.onblur = function () {
  49. if (this.value.length === 0) {
  50. alert('密码不能为空');
  51. return false;
  52. }
  53. };
  54. </script>
  55. </body>
  56. </html>

css

  1. body {
  2. /* background-color: aqua; */
  3. background-image: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp08%2F01042323313046.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651903778&t=a6d0d4182431e70824110551caa00dd6);
  4. image-rendering: repeat - x;
  5. color: #444;
  6. font-weight: lighter;
  7. }
  8. #login {
  9. width: 20em;
  10. border-radius: 00.3em;
  11. box-shadow: 0 0 1em;
  12. box-sizing: border-box;
  13. padding: 1em 2em 1em;
  14. margin: 5em auto;
  15. background-color: aquamarine;
  16. display: grid;
  17. grid-template-columns: 3em 1fr;
  18. gap: 1em 0;
  19. }
  20. #login .title {
  21. grid-area: auto / span 2;
  22. place-self: center;
  23. }
  24. #login .input {
  25. border-radius: 0.3em;
  26. border: none;
  27. padding-left: 0.3em;
  28. }
  29. #login input:focus {
  30. outline: none;
  31. box-shadow: 0 0 5px seagreen;
  32. background-color: hsl(283, 100%, 95%);
  33. }
  34. #login button {
  35. grid-area: auto / 2 / auto;
  36. outline: none;
  37. background-color: lightseagreen;
  38. border: none;
  39. color: aliceblue;
  40. }
  41. #login button:hover {
  42. cursor: pointer;
  43. background-color: seagreen;
  44. transition: 0.5s;
  45. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议