简体   繁体   English

如何使用JavaScript或jQuery将样式表中CSS类的属性直接加载到哈希中?

[英]How can I load the attributes of a CSS class in a stylesheet directly into a hash using JavaScript or jQuery?

I want to load the key value pairs from a class, or id, in a CSS stylesheet into a JavaScript object in order to access the data. 我想将CSS样式表中的类或id的键值对加载到JavaScript对象中,以便访问数据。

Note that I do NOT want to add that class to a DOM element (at least not directly) 请注意,我不想将该类添加到DOM元素中(至少不是直接添加)

The only way I can see in JQuery is to create a dummy hidden element, add my class to it using 我在JQuery中看到的唯一方法是创建一个虚拟的隐藏元素,使用添加其类

$(“#dummy").addClass(“myclass”);

and then query that using 然后使用查询

$(“#dummy”).(cssproperty);

But even there I want to see each css property, without knowing what they are in advance. 但是,即使在那儿,我也想查看每个CSS属性,而不事先知道它们的含义。

What I really need is something simple that loads a CSS class into a hash. 我真正需要的是将CSS类加载到哈希中的简单操作。 I'm not seeing that in jQuery... is there something in regular JavaScript? 我没有在jQuery中看到...常规JavaScript中有东西吗?

thanks --Rob 谢谢-罗布

Two options for you: 您有两个选择:

1) The Stylesheet object 1) Stylesheet对象

...discussed here and here . ...在这里这里讨论。 That gives you direct access to the class definition, from which you can read the style properties directly and apply them to your DOM element. 这使您可以直接访问类定义,从中可以直接读取样式属性并将其应用于DOM元素。

Sadly, I don't have time to dash off an example right now, but the links above have them. 可悲的是,我现在没有时间举一个例子,但是上面的链接有它们。 Basically, you loop through the stylesheets ( document.styleSheets ) and for each stylesheet, loop through the cssRules ( rules on some browsers). 基本上,您遍历样式表( document.styleSheets ),对于每个样式表,遍历cssRules (某些浏览器上的rules )。 Each rule has a selectorText telling you what its selector looks like (".foo" or whatever). 每个规则都有一个selectorText告诉您其选择器的外观(“ .foo”或其他内容)。 When you find the rule with the selector matching your class, you can loop through the properties on its style property using for (name in rule.style) , with the value being rule.style[name] inside the loop. 当找到带有与您的类匹配的选择器的规则时,可以使用for (name in rule.style)style属性上的属性,该值在循环内为rule.style[name]

2) getComputedStyle : 2) getComputedStyle

Do as you said and apply the class to an off-page element, then use the getComputedStyle ( MDC , MSDN ) function to get the style object for its computed style. 按照您说的做,并将类应用于页面外元素,然后使用getComputedStyleMDCMSDN )函数获取其计算样式的style对象。 Be careful, though, as the computed style may have items that are inherited (eg, from styles applied to all children of body or even html ). 但是要小心,因为计算的样式可能具有继承的项(例如,从应用于body所有子项甚至是html样式)。

TJ Crowder gave me the answer on this one. TJ Crowder给了我这个答案。 Here is some code that implements the thing I was looking for: 这是一些实现我想要的东西的代码:

Step 1: get the stylesheet and the specific rule that I'm interested in: 第1步:获取样式表和我感兴趣的特定规则:

var stylesheets = document.styleSheets;
var rule = document.styleSheets[0].cssRules[0];

Step 2: Access that specific rule in various ways 步骤2:以各种方式访问​​该特定规则

rule.style.cssText  // return the full text of that CSS rule
rule.selectorText   // return the selector for that rule e.g. .myclass

Loop through the individual styles: 循环浏览各个样式:

var styleObj = rule.style;

for (var i = 0; i < styleObj.length; i++) {
  var key = styleObj[i];             // key is the CSS property e.g. 'color'
  var val = styleObj[key.toCamel()]  // val is the CSS value e.g. 'red'
}

NOTE that you have to convert the CSS property to CamelCase for those with hyphens. 注意,对于那些带有连字符的用户,必须将CSS属性转换为CamelCase。 For example 'font-family' must be converted to 'fontFamily' 例如,“ font-family”必须转换为“ fontFamily”

For some reason JavaScript and jQuery do not have functions for doing this (Prototype does) so you should stick these extensions to the String class somewhere in your script 出于某种原因,JavaScript和jQuery没有执行此操作的功能(Prototype具备),因此您应将这些扩展名放在脚本中某个字符串类上

String.prototype.toCamel = function(){
return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
};
String.prototype.toDash = function(){
return this.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
};

I got that from James Roberts' blog 我是从詹姆斯·罗伯茨的博客中得到的

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

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