Blogger Information
Blog 94
fans 0
comment 0
visits 92651
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】 dataset对象 、操作CSS 与 classList对像:操作元素
可乐随笔
Original
563 people have browsed it

dataset对象 、操作CSS 与 classList对像:操作元素

1.dataset对象

  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>dataset对象</title>
  8. </head>
  9. <body>
  10. <!--
  11. * 1. 预定义:id,class,style,title...
  12. * 2. 自定义:data- 前缀
  13. -->
  14. <!-- data-: 自定义属性的前缀 -->
  15. <div data-email="nx99@qq.com" data-my-name="老马">我的邮箱</div>
  16. <script>
  17. const div = document.querySelector('div');
  18. // console.log(div.dataset['email']);//同下
  19. console.log(div.dataset.email);
  20. //my-name:转为小驼:myName
  21. console.log(div.dataset.myName)
  22. </script>
  23. </body>
  24. </html>

2.操作CSS

关键问题:
1.找到元素
2.操作元素的行内样式

  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>操作CSS</title>
  8. <style>
  9. div{
  10. width: 200px;
  11. height: 50px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="color:red;background-color:lightblue">hello world</div>
  17. <script>
  18. //获取行内样式style
  19. const div = document.querySelector('div');
  20. console.log(div.style.color);
  21. console.log(div.style.backgroundColor);
  22. //文档样式用style获取不到
  23. // console.log(div.style.width)
  24. //一个元素,最终样式,也叫计算样式,是由多个类型的样式构成:外部、文档、行内样式
  25. //拿到一个元素的计算样式:getComputedStyle(元素)
  26. console.log(window.getComputedStyle(div).width);
  27. //取数值
  28. let width = window.getComputedStyle(div).width;
  29. width = parseInt(width);
  30. console.log(width,typeof width);
  31. //给Div增加宽度,修改div的行内样式
  32. div.style.width = width + 100 + 'px';
  33. console.log(window.getComputedStyle(div).width);
  34. </script>
  35. </body>
  36. </html>

3.classList对像:操作元素

classList:管理元素的class属性

方法:
1.add():添加
2.remove():移除
3.replace():替换
4.toggle():切换
5.contains():判断

  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>classList对像:操作元素</title>
  8. </head>
  9. <body>
  10. <h1>hello world!</h1>
  11. <style>
  12. .red {
  13. color:red;
  14. }
  15. .blue {
  16. color:blue;
  17. }
  18. .bgc {
  19. background-color: yellow;
  20. }
  21. </style>
  22. <script>
  23. //classList:管理元素的class属性
  24. const h1 = document.querySelector('h1');
  25. //1. 添加:add()
  26. // h1.classList.add('red');
  27. // h1.classList.add('blue','bgc');
  28. const styles = ['red','bgc'];
  29. h1.classList.add(...styles);
  30. //2.判断:contains()
  31. // 判断是否有某个样式 console.log(h1.classList.contains('bgc'))
  32. //3.替换:replace()
  33. h1.classList.replace('red','blue');
  34. //4.移除: remove()
  35. h1.classList.remove('blue','bgc');
  36. //5.切换:toggle()
  37. //切换:有该class,就remove();没有就add();
  38. h1.classList.toggle('red');
  39. h1.classList.toggle('red');
  40. </script>
  41. </body>
  42. </html>
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