Blogger Information
Blog 13
fans 0
comment 0
visits 6515
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ClassList对象、表单事件
shooter
Original
603 people have browsed it

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. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!