Get inline-style value even if element has the value also set in CSS?

前端 未结 2 1197
小鲜肉
小鲜肉 2021-01-18 23:39

If you have the following element:


and the css is:

span {width:50px!importa         


        
2条回答
  •  不知归路
    2021-01-19 00:06

    To find the applied style:

    var span = document.querySelector('span[style]'),
        actualWidth = window.getComputedStyle(span, null).width;
    

    Note that this gets the style which won, which in this case will be the one with the !important modifier. If you must instead try and retrieve the in-line style, regardless of whether it was applied or not, you can simply use the style object of the element node:

    inlineWidth = span.style.width;
    

    JS Fiddle demo.

    Do remember that, for width to be applied to a span element, it must be either display: block or display: inline-block.

提交回复
热议问题