简体   繁体   English

使用JavaScript或jQuery修改CSS规则?

[英]Modify CSS rules with JavaScript or jQuery?

Is it possible to edit/modify existing css rules using javascript or jquery? 是否可以使用javascript或jquery编辑/修改现有的CSS规则? For instance, the stylesheet has this class: 例如,样式表具有此类:

.red {
    color: red;
}

And I have 10 elements using the class. 我有10个使用该类的元素。

What I would love to do, is to edit the class like this: 我想做的是像这样编辑类:

.red {
    color: blue;
}

So that it will also effect the future instances of the same class. 这样它也将影响同一类的将来实例。 (11th, 12th, 13th elements and so on). (第11、12、13个元素,依此类推)。

This is how you change the CSS rule using JavaScript. 这是使用JavaScript更改CSS规则的方式。

 var ss = document.styleSheets; var m = document.getElementById('modifiedRule'); for (i = 0; i < ss.length; i++) { var rules = ss[i]; for (j = 0; j < rules.cssRules.length; j++) { var r = rules.cssRules[j]; if (r.selectorText == ".red") { m.innerHTML = "<br />Old rule: " + r.cssText; r.style.color = "blue"; m.innerHTML += "<br />Modified rule: " + r.cssText; } } } 
 .red { color: red; } 
 <div class="red">Text</div> <div class="red">Text</div> <div class="red">Text</div> <div class="red">Text</div> <div class="red">Text</div> <div class="red">Text</div> <div class="red">Text</div> <div id="modifiedRule"></div> 

Javascript provides the document.stylesheets collection which provides a list of CSSStyleSheet objects. Javascript提供了document.stylesheets集合,该集合提供了CSSStyleSheet对象的列表。 You can traverse this collection to find any specific css rule you are interested in and modify its rules. 您可以遍历此集合以找到您感兴趣的任何特定CSS规则并修改其规则。

However, as mentioned before me this is probably not the correct approach for what you are trying to achieve. 但是,正如我之前提到的,这可能不是您要实现的正确方法。 It can be a bit browser depenedant and just feels hacky. 它可能有点依赖浏览器,但感觉很笨拙。

You would be better off having 2 separate rules 您最好有2条单独的规则

.red { color :red }
.blue { color : blue }

Inside your javascript maintain a variable that holds the current default class for your elements, ie. 在您的JavaScript中,维护一个变量,该变量保存元素的当前默认类,即。 current_class = 'blue'. current_class ='蓝色'。 When you create an element simply .addClass(current_class). 创建元素时,只需添加.addClass(current_class)。 When you want to change all the elements, .toggleClass(current_class, new_class). 当您想更改所有元素时,.toggleClass(current_class,new_class)。

Seems to me you could just do this: 在我看来,您可以这样做:

var reds = $('.red');
var index = 1;
reds.each(function(){
    $(this).css('color', index > 10 ? 'blue' : 'red');
    index++;
})

What you're asking seems to me like an abuse of CSS (regardless of whether or not you find a workable solution to you specific question). 在我看来,您要问的是对CSS的滥用(无论您是否针对特定问题找到了可行的解决方案)。

I'd approach this as follows: 我会这样处理:

.red .myElementClass{ /*or perhaps .red>.myElementClass*/
    color:red;
}
.blue .myElementClass{ /*or perhaps .blue>.myElementClass*/
    color:blue;
}

<div id="outer" class="red">
    <div class="myElementClass">foo</div>
    <div class="myElementClass">foo</div>
    <div class="myElementClass">foo</div>
    <div class="myElementClass">foo</div>
    <div class="myElementClass">foo</div>
    <div class="myElementClass">foo</div>
</div>

Now you can add as many as you want, and to change all of their colors, just swap the class of the div#outer to blue . 现在,您可以根据需要添加任意数量,并更改其所有颜色,只需将div#outer的类交换为blue

I believe jQuery .css() is what you are looking for. 我相信jQuery .css()是您要找的东西。

$('.red').css('color', 'blue');

http://api.jquery.com/css/ http://api.jquery.com/css/

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

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