How can I access style properties on javascript objects which are using external style sheets?

前端 未结 4 958
长发绾君心
长发绾君心 2021-01-13 10:49

I have an external style sheet with this in it:

.box {
padding-left:30px;
background-color: #BBFF88;
border-width: 0;
overflow: hidden;
width: 400px;
height:         


        
4条回答
  •  长情又很酷
    2021-01-13 11:28

    You should use window.getComputedStyle to get that value. I would recommend against using offsetWidth or clientWidth if you're looking for the CSS value because those return a width which includes padding and other calculations.

    Using getComputedStyle, your code would be:

    var e = document.getElementById('0');
    var w = document.defaultView.getComputedStyle(e,null).getPropertyValue("width");
    

    The documentation for this is given at MDC : window.getComputedStyle

提交回复
热议问题