Blogger Information
Blog 14
fans 0
comment 0
visits 7540
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
classList 和 事件
Bruce.lau
Original
488 people have browsed it

classList 和 事件

预览地址事件效果

classList使用

  1. <title>JS操作class</title>
  2. <div>
  3. <ul class="list">
  4. <li>item1</li>
  5. <li>item2</li>
  6. <li>item3</li>
  7. </ul>
  8. </div>
  9. <style>
  10. .list {
  11. background-color: cornsilk;
  12. height: 300px;
  13. }
  14. .ulwidth{
  15. width: 500px;
  16. }
  17. li {
  18. color: red
  19. }
  20. </style>
  21. <script>
  22. //行内样式使用xxx.style.xxx进行操作
  23. // 内部样式,外联样式使用 window.getComputedStyle()操作
  24. //例如:增加ul边框,
  25. let ul = document.querySelector('.list');//先获取ul
  26. ul.style.border = '1px solid';//对行内样式经行更改
  27. console.log(ul.style.border);//输出 1px solid
  28. console.log(ul.style.height);//height属性 在内联样式,没有输出结果,获取失败
  29. console.log(window.getComputedStyle(ul).height);//输出 高度300px
  30. //给高度增加100,先要将字符串转换成数字,使用paresInt()转换,然后赋值
  31. let height1 = parseInt(window.getComputedStyle(ul).height);
  32. height1 = height1 + 100;
  33. console.log(height1);
  34. ul.style.height = (height1 + 'px');
  35. console.log(window.getComputedStyle(ul).height);//输出 高度400px,修改成功
  36. let li = document.querySelector('li');//先获取li
  37. console.log(li.style.color);//color属性在内联样式,没有输出结果,获取失败
  38. // 内部样式,外联样式使用 window.getComputedStyle()获取值
  39. console.log(window.getComputedStyle(li).color);//输出 rgb(255,0,0)
  40. //使用classList操作属性对象
  41. //例如 给ul添加宽度属性
  42. ul.classList.add('ulwidth');
  43. ul.classList.add('list');
  44. console.log(window.getComputedStyle(ul).width);//输出500px添加成功
  45. //移除 classList.remove
  46. //替换 classList.replace('旧属性','新属性')
  47. //切换 没有就添加,有就删除 classList.toggle()
  48. </script>

第二部分 事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件</title>
  6. </head>
  7. <body>
  8. <form action="" method="get" name="login">
  9. <label for="name">账号</label>
  10. <input type="text" name="name" id="name" placeholder="请输入账号" autofocus>
  11. <div class="tishi">请输入账号</div>
  12. <div></div>
  13. <label for="pwd">密码</label>
  14. <input type="password" name="pwd" id="pwd" placeholder="请输入账号" >
  15. </form>
  16. <script>
  17. let form = document.forms.login;
  18. let account= form.elements.name;
  19. //account = 'admin';
  20. console.log(account.value)
  21. let pwd= form.elements.pwd
  22. // pwd.focus()
  23. console.log(pwd)
  24. //失去焦点的处理
  25. account.onblur=function () {
  26. let tishi = document.querySelector('.tishi')
  27. if (account.value.length===0){
  28. tishi.style.display='block'
  29. account.focus()
  30. }else{
  31. tishi.style.display='none'
  32. }
  33. }
  34. </script>
  35. <style>
  36. .tishi{
  37. display: none;
  38. }
  39. </style>
  40. </body>
  41. </html>
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