Get current applied style with JS

后端 未结 1 1773
夕颜
夕颜 2020-12-11 07:12

I was looking at this post How to get the background color of an element using javascript?

which confirmed that if you do

div.style.color 


        
相关标签:
1条回答
  • 2020-12-11 08:08

    You need to use getComputedStyle() to get the styles assigned through the stylesheet.

    console.log(getComputedStyle(document.getElementById('content')).backgroundColor);
    console.log(document.getElementById('content').style.color);
    #content {
      background-color: #000000;
      display: inline-block;
      height: 100px;
      width: 100px;
    }
    <div id="content" style="color:red;"></div>

    0 讨论(0)
提交回复
热议问题