Reading non-inline CSS style info from Javascript

前端 未结 4 1154
栀梦
栀梦 2020-12-03 09:16

I know I must be missing something here, but I cannot seem to get this to work.

I have assigned a background color to the body of an html document using the style ta

4条回答
  •  余生分开走
    2020-12-03 09:43

    The style property of a DOM element refers only to the element's inline styles.

    Depending on the browser, you can get the actual style of an element using DOM CSS

    In firefox, for example:

    var body = document.getElementsByTagName("body")[0];
    var bg = window.getComputedStyle(body, null).backgroundColor;
    

    Or in IE:

    var body = document.getElementsByTagName("body")[0];
    var bg = body.currentStyle.backgroundColor;
    

提交回复
热议问题