最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

If you have the following element:

<span style="width:100px"></span>

and the css is:

span {width:50px!important;}

Is there a way I can get the inline-style value and ignore the css value?

Thanks

If you have the following element:

<span style="width:100px"></span>

and the css is:

span {width:50px!important;}

Is there a way I can get the inline-style value and ignore the css value?

Thanks

Share Improve this question asked Oct 2, 2013 at 12:25 user2413333user2413333 1,4054 gold badges18 silver badges22 bronze badges 2
  • no as long as you have !important.. – Dipesh Parmar Commented Oct 2, 2013 at 12:27
  • You are probably going to need display:inline-block to use width with an inline element. – fred02138 Commented Oct 2, 2013 at 12:30
Add a comment  | 

2 Answers 2

Reset to default 13

use element.style.width

sample:

$(function(){
    alert($("span")[0].style.width);
});

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.

发布评论

评论列表(0)

  1. 暂无评论