【JavaScript】DOM之样式操作

小灰灰 2023-01-23 11:58 34阅读 0赞

样式操作

获取内联样式

以节点和元素的方法来获取指定的CSS样式

  1. <body>
  2. <div id="q1" style="width: 200px;height: 200px;border: 1px solid black"></div>
  3. <script>
  4. var div = document.getElementById('q1');
  5. // 节点属性获取指定元素的属性
  6. var attrNode = div.getAttributeNode('style');
  7. var style = attrNode.nodeValue;
  8. console.log(style);
  9. var div = document.getElementById('q1');
  10. // 元素属性获取指定的元素的属性
  11. var style = div.getElementById('style');
  12. console.log(style);
  13. var div = document.getElementById('q1');
  14. var style = div.style;
  15. console.log(style.width);
  16. </script>
  17. </body>

希望能够帮助大家,另外小编谢谢大家一如既往的支持,欢迎大家一起和群里的小伙伴一起学习,交流,群里面每天都有分享初学者的笔记资料,视频。点击进入暗号:知乎

CSS StyleDeclaration对象

以getPropertyVslue()方法通过elementstylestyleitem.getPropertyVslue()获取CSS样式属性值
该属性还具有length属性

  1. <body>
  2. <div id="q1" style="width: 200px;height: 200px;border: 1px solid black"></div>
  3. <script>
  4. var div = document.getElementById('q1');
  5. var style = div.style;
  6. // 得到CSSStyleDeclaration对象
  7. console.log(style.cssText);
  8. // cssText属性;获取当前元素中所有的样式属性内容(字符串类型)
  9. console.log(style.length);
  10. // length属性;获取当前元素中样式属性的个数(不等于与设置的个数一致)
  11. var attrName = (style.item);
  12. console.log(attrName);
  13. console.log(style.getPropertyCSSValue(attrName));
  14. // getPropertyCSSValue(Name);获取当前元素中指定属性名对应的样式属性值
  15. for (var attr in style) {
  16. console.log(attr);//遍历该对象
  17. }
  18. </script>
  19. </body>

程序猿的生活:JS实现字体逐个打印​zhuanlan.zhihu.com

获取外联样式表

以styleSheets属性,该属性返回所有包含外联样式表返回包括内嵌样式表合外联样式表的集合

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>获取外联样式</title>
  4. <link rel="stylesheet" href="style.css">
  5. <style>
  6. .q1{
  7. width:200px;
  8. height: 200px;
  9. background-color: blue;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="d1"></div>
  15. <div class="d2"></div>
  16. <script>
  17. var stylSheetList = document.styleSheets;
  18. // 获取当前所有页面的样式表
  19. // 并返回StyleSheetList对象(类数组对象)
  20. console.log(document.styleSheets);
  21. var styleSheet = styleSheetList[0];
  22. /*
  23. * type属性 - 表示当前使用的是CSS样式
  24. href属性 - 表示当前样式表的路径
  25. cssRules/rules属性 - 表示当前样式表中所有的样式规则
  26. */
  27. console.log(styleSheet);
  28. var cssRuleList = styleSheet.rules;
  29. console.log(cssRuleList);
  30. // 表示当前样式表中的所有规则集合(类数组对象)
  31. var styleDecl = cssStyleRule.style;
  32. console.log(styleDecl);
  33. </script>
  34. </body>

获取class属性

以className属性来获取页面中元素的class属性值,Element对象该属性不是个class属性值可获取页面中元素的class属性值的列表

以classList属性来获取页面中元素的class属性值,在页面中元素的class属性值为多个样式,该属性可获取页面中元素的class属性值的列表

  1. <style>
  2. .q1 {
  3. width:200px;
  4. height: 200px;
  5. background-color: blue;
  6. }
  7. .q2 {
  8. font-family: 新宋体;
  9. color: darkgray;
  10. }
  11. .two {
  12. width: 200px;
  13. height: 200px;
  14. background-color: blue;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="q1" class="q1 q2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores autem delectus dolores enim ex iusto non optio quibusdam! A animi minima nisi repudiandae rerum voluptas! At cumque iste nulla quasi.</div>
  20. <script>
  21. var div = document.getElementById('q1');
  22. var className = div.className;
  23. // DOM中的对象的className属性并得到class属性值
  24. console.log(className);
  25. // div.className = 'two';
  26. var attr = div.getAttribute('class');
  27. console.log(attr);
  28. // div.setAttribute('class','two');
  29. var classList = div.classList;
  30. // classList属性(浏览器兼容问题)
  31. console.log(classList);
  32. </script>
  33. </body>

获取当前有效样式

以Window对象表示在getComputedStyle()方法来获取元素当前有效样式,并带有浏览器的兼容问题

  1. <style>
  2. .q1 {
  3. color: blue;
  4. font-family: 新宋体;
  5. }
  6. </style>
  7. </head>
  8. <body>
  9. <div id="q1" style="width: 200px;height: 200px;background-color: red;" class="q1">火影忍者</div>
  10. <script>
  11. var div = document.getElementById('q1');
  12. var style = div.currentStyle;
  13. console.log(style);
  14. // 在window对象的getComputedStyle(element)
  15. // element是指定元素
  16. // * 返回值为CSSStyleDeclaration对象
  17. // * 此方法仅用于获取,不能用于设置
  18. function getStyle(element) {
  19. if(element.getComputedStyle){
  20. return window.getComputedStyle(element);
  21. // 具有兼容问题
  22. } else {
  23. return element.currentStyle;
  24. }
  25. }
  26. </script>
  27. </body>

程序猿的生活:我从来不理解JavaScript闭包,直到有人这样向我解释它…​zhuanlan.zhihu.com

设置内联样式

style属性;通过页面元素的style属性实现
setAttirbute()方法;通过style属性设置,还可调用以elementsetAttribute()方法实现

  1. <body>
  2. <div id="q1" style="width: 200px;height: 200px;border: 1px solid black"></div>
  3. <script>
  4. var div = document.getElementById('q1');
  5. div.setAttribute('style','width: 400px;height: 400px;border: 1px solid black');
  6. var div = document.getElementById('q1');
  7. var style = div.style;
  8. style.width ='600px';
  9. style.height = '600px';
  10. </script>
  11. </body>

Element对象的样式属性

元素内部的宽度和高度

以Element对象的clientWidth和clientheiht来设置内部的宽度和高度

  1. <style>
  2. #q1{
  3. width: 200px;
  4. height: 200px;
  5. border: 10px solid black;
  6. box-sizing: border-box;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <script>
  12. var div = document.getElementById('q1');
  13. console.log(div.clientWidth, div.clientHeight);
  14. // 内容区 + 内边距;只能获取,不能设置
  15. </script>
  16. </body>

内容区的宽度和高度

以Element对象的scrollWidth和scrollheiht来设置内容区的宽度和高度

  1. <style>
  2. #fu{
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. overflow: auto;
  7. }
  8. #zi{
  9. width: 400px;
  10. height: 400px;
  11. background-color: blue;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="fu">
  17. <div id="zi"></div>
  18. </div>
  19. <script>
  20. var fu = document.getElementById('fu');
  21. console.log(fu.scrollWidth, fu.scrollHeight);
  22. </script>
  23. </body>

滚动条滚动的宽度和高度

以Element对象的scrolleft属性来设置滚动条到元素的距离

  1. <style>
  2. #fu{
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. overflow: auto;
  7. }
  8. #zi{
  9. width: 400px;
  10. height: 400px;
  11. background-color: blue;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="fu">
  17. <div id="zi"></div>
  18. </div>
  19. <script>
  20. var fu = document.getElementById('fu');
  21. parent.onscroll = function () {
  22. console.log(fu.scrollLeft, fu.scrollTop);
  23. }
  24. </script>
  25. </body>

程序猿的生活:js浮点数加减乘除​zhuanlan.zhihu.com

判断元素内容是否滚动到底

以Element对象的scrollTop属性来设置滚动条到元素顶部的距离

  1. <style>
  2. #fu{
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. overflow: auto;
  7. }
  8. #zi{
  9. width: 400px;
  10. height: 400px;
  11. background-color: blue;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="fu">
  17. <div id="zi">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto distinctio ex expedita fugiat harum iusto maxime minus mollitia nesciunt praesentium quae quas, qui quisquam sint sunt ullam vitae voluptate voluptatem?</div>
  18. </div>
  19. <script>
  20. var fu = document.getElementById('fu');
  21. fu.onscroll = function () {
  22. console.log(fu.clientHeight, fu.scrollHeight, fu.scrollTop);
  23. }
  24. </script>
  25. </body>

获取指定元素的定位父元素
以Element对象的scrollheight,scrolltop,和clientheight来判断是否滚到底

  1. <style>
  2. #fu{
  3. position: relative;
  4. }
  5. #zz{
  6. position: relative;
  7. }
  8. #q1 {
  9. width: 100px;
  10. height: 100px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="fu">
  17. <div id="zi"></div>
  18. </div>
  19. <script>
  20. var q1 = document.getElementById('q1');
  21. /*
  22. 1.如果当前元素的所有祖先元素都没有开启定位的话 - <body>元素
  23. 2.如果当前元素的所有祖先元素中,只能一个开启定位的话 - 开启定位的祖先元素
  24. 3.如果当前元素的所有祖先元素中,具有多个开启定位的话 - 距离当前元素最近的那个祖先元素
  25. */
  26. console.log(q1.offsetParent);

发表评论

表情:
评论列表 (有 0 条评论,34人围观)

还没有评论,来说两句吧...

相关阅读