Get a CSS value with JavaScript

前端 未结 9 1444
温柔的废话
温柔的废话 2020-11-21 07:55

I know I can set a CSS value through JavaScript such as:

document.getElementById(\'image_1\').style.top = \'100px\';

But, can I

9条回答
  •  花落未央
    2020-11-21 08:15

    You can use getComputedStyle().

    var element = document.getElementById('image_1'),
        style = window.getComputedStyle(element),
        top = style.getPropertyValue('top');
    

    jsFiddle.

提交回复
热议问题