<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> .box{ width: 200px; height: 200px; background-color: red; } </style> </head> <body> <div class="box"></div> <script type="text/javascript"> let qq=document.querySelector("div"); //在js中设置类是这样设置的.第一种 /*qq.className="name";//替换原来的类 //第二种: qq.style.width="333px"; qq.style.height="333px"; qq.style.backgroundColor="yellow"; */ //获取样式 let ww= document.querySelector("div"); //console.log(ww.style.width);//只能获取行内的样式,获取不到css设置的样式. // 注意点: 如果想获取到CSS设置的属性值, 必须通过getComputedStyle方法来获取 // getComputedStyle方法接收一个参数, 这个参数就是要获取的元素对象 // getComputedStyle方法返回一个对象, 这个对象中就保存了CSS设置的样式和属性值 let style=window.getComputedStyle(ww); console.log(style.width); </script> </body> </html>
dom啦7设置 获取 元素的样式
关注
打赏