简体   繁体   English

使用javascript / jquery从未计算的CSS样式中获取价值

[英]Get value from uncomputed css style with javascript / jquery

I have a small question concerning css styles and javascript / jquery. 我有一个关于CSS样式和javascript / jquery的小问题。 Lets say i define the following css style. 可以说我定义以下css样式。

.myStile { padding: 5px }

Now I'd like to access the property of padding while not writing an html element to the document. 现在,我想在不将html元素写入文档的同时访问padding的属性。 I came up with the following idea which only works with Firefox (IE and Chrome say NaN) 我提出了以下想法,该想法仅适用于Firefox(IE和Chrome说NaN)

var div = $('<div class="myStile />');
var padding = parseInt(div.css('padding-left'), 10);

is there a way to make this work in IE and Chrome (well trident and webkit I guess) as well? 有没有办法在IE和Chrome(我想也是三叉戟和Webkit)中实现这一目标? I know that one's able to read whole css files via javascript (and get the class, the properties and so on ...), but that seems to be a bit overkill. 我知道一个人可以通过javascript读取整个CSS文件(并获取类,属性等...),但这似乎有些矫kill过正。

Thanks meo for your comment. 谢谢您的评论。 This post answers the question. 这篇文章回答了这个问题。 Here's a google find which answers the "other browsers than chrome, firefox, safari" issue. 这是一个Google搜索结果 ,它回答了“ Chrome,firefox,safari以外的其他浏览器”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM