使用style获取元素及设置元素样式注意点

可以通过style可以给元素设置样式的。

需要注意的是:style是一个对象,只能获取行内样式(写在标签里的),不能获取内嵌的样式和外链的样式。

 <div style="height:100px;width:100px; background-color:pink;" class="box">style</div>

  .box{
    
    
     
      color: orange;
    }
  mounted(){
    
    
 let ele = document.querySelector('.box');
 console.log(ele.style.height);// 内联样式,打印为100px
 console.log(ele.style.height);// 内联样式,打印为100px
 console.log(ele.style.color);// 通过clssName设置,非内联样式,获取不到,是空值
 console.log(ele.style.backgroundColor);// 内联样式,打印为pink。

结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42931285/article/details/124418982
今日推荐