是否可以使用javascript读取DOM中未使用的css类的样式?
我需要能够阅读一个加载的网页可用的css类。不是分配给DOM元素的类,而是CSS中定义的类。这是否可以通过javascript实现,或者特别是jQuery实现 我正试图用一种骇人的方式覆盖我正在使用的CMS(concrete5)中内置的列,所以请容忍我。加载时,将有许多具有特定内联百分比宽度的div,例如是否可以使用javascript读取DOM中未使用的css类的样式?,javascript,jquery,css,Javascript,Jquery,Css,我需要能够阅读一个加载的网页可用的css类。不是分配给DOM元素的类,而是CSS中定义的类。这是否可以通过javascript实现,或者特别是jQuery实现 我正试图用一种骇人的方式覆盖我正在使用的CMS(concrete5)中内置的列,所以请容忍我。加载时,将有许多具有特定内联百分比宽度的div,例如style=“width:27%;”。在我的css中,有许多网格系统的类,如.one,.two,.two,.three,等等,我希望与这些div相匹配 例如,我在这里有3个css类: .one
style=“width:27%;”
。在我的css中,有许多网格系统的类,如.one
,.two
,.two
,.three
,等等,我希望与这些div相匹配
例如,我在这里有3个css类:
.one { width: 8.3333%; }
.two { width: 16.6667%; }
.three { width: 25%; }
我的一个div的内联样式为
width:22%代码>。我想给它分配最匹配的类,在本例中是.three
,并以编程方式删除内联样式。当.one、.two、.three等的样式没有附加到元素时,我有没有办法读取它们的样式,换句话说,直接从css读取它们?我希望避免硬编码这些值,因为它们可能会更改。CSS类的宽度可以如下获得:
function getClassWidth(className) {
var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
for (var i = 0; i < classes.length; i++) {
if (classes[i].selectorText == className) {
return classes[i].style.getPropertyValue("width");
}
}
}
getClassWidth('.one'); // Would return the string "8.3333%"
函数getClassWidth(className){
var classes=document.styleSheets[0]。规则| | document.styleSheets[0]。cssRules;
对于(var i=0;i
解决方案基于。CSS类的宽度可以如下获得:
function getClassWidth(className) {
var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
for (var i = 0; i < classes.length; i++) {
if (classes[i].selectorText == className) {
return classes[i].style.getPropertyValue("width");
}
}
}
getClassWidth('.one'); // Would return the string "8.3333%"
函数getClassWidth(className){
var classes=document.styleSheets[0]。规则| | document.styleSheets[0]。cssRules;
对于(var i=0;i
解决方案基于