简体   繁体   English

添加/删除CSS类属性以尊重不基于Div ID的类

[英]Add/Remove CSS class attributes to respect to the class not based on Div id

I have a CSS elements defined as below: 我有一个CSS元素定义如下:

.myRedClass
{
  background-color:red;
}

And it is assigned to many Divs( that are having similar appearance). 并且它被分配给许多Divs(具有相似的外观)。 At some point i need to assign some left-margin also. 在某些时候,我还需要分配一些左边距。 What i am currently doing is; 我目前正在做的是 I will add and remove a Predefined Class .redClass-margin{margin-left:10px} to each of the required div. 我将在每个所需的div中添加和删除预定义的类.redClass-margin{margin-left:10px} The code for this is: 的代码是:

$( "myDiv1" ).addClass( "redClass-margin" );
$( "myDiv2" ).addClass( "redClass-margin" );
$( "myDiv3" ).addClass( "redClass-margin" );

and so on... Instead for this Can i do changes in the existing css class. 依此类推...为此,我可以在现有的CSS类中进行更改吗? Is their any possibility to change the .myRedClass like the following : 他们是否有可能像下面这样更改.myRedClass

.myRedClass
    {
      background-color:red;
      margin-left:10px;
    }

Either Through script or through c# 通过脚本或通过c#

You can't edit an existing css stylesheet. 您无法编辑现有的CSS样式表。

There's a different approach though. 但是有另一种方法。 If you just want to find all divs that have class myRedClass , and add redClass-margin , you can find them all with jquery and add class. 如果只想查找所有具有myRedClass类的div并添加redClass-margin ,则可以使用jquery并添加类来查找它们。

$('.myRedClass').addClass('redClass-margin');

I really don't get why you want to do this I would just create a new classand append the class name to each element like Eric mentioned. 我真的不明白为什么要这样做,我只是创建一个新类,然后将类名附加到每个提到的元素上,如Eric所述。

However there is a here that tell you how to do what your asking. 但是, 这里有一个告诉您如何处理您的要求的信息。

Alternatively you could inject rules into your style sheet, again a weird way to do it. 或者,您可以将规则注入样式表中,这也是一种怪异的方式。 See this . 看到这个

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

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