简体   繁体   English

使用jQuery更改div类

[英]Changing div class with jQuery

I try to develop some charts which should have online values. 我尝试开发一些应该具有在线价值的图表。 I have found this nice and free dashboard : [ http://jiji262.github.io/Bootstrap_Metro_Dashboard/][1] I use Web sockets to change values of charts and it works very nice. 我发现了一个不错的免费仪表板:[ http://jiji262.github.io/Bootstrap_Metro_Dashboard/] [1 ]我使用Web套接字更改图表的值,并且效果很好。 I use justgage([ http://justgage.com/][2] ) plug-in. 我使用justgage([ http://justgage.com/] [2 ] )插件。 As you can see there are some square shapes with different colors(Disk Space Usage,Bandwidth,memory,CPU etc.) and i want to change color of these squares according to values. 如您所见,有些正方形具有不同的颜色(磁盘空间使用率,带宽,内存,CPU等),我想根据值更改这些正方形的颜色。

Here is div construction : 这是div施工:

<div class="span2" onTablet="span4" onDesktop="span2"> 
            <div ID="ID1" class="circleStatsItemBox green"> 
            <div class="header">Machine 1</div> 
              <span class="percent"></span> 
            <div class="circleStat">    
               <div id="g1"></div>
            </div>
            <div id="f1" class="footer">
                <span class="number">cards/hour</span>
            </div>
            </div>
</div>

g1 is justgage gauge and i use this code to change div class : (to change of square) g1是量规,我使用此代码更改div类:(更改正方形)

$('#ID1').addClass('circleStatsItemBox green').removeClass('circleStatsItemBox orange'); $('#ID1')。addClass('circleStatsItemBox绿色').removeClass('circleStatsItemBox橙色');

But this code changes title and footer color as same with body color. 但是此代码将标题和页脚颜色更改为与主体颜色相同。 I want that footer and header color should be arranged according to new div class (for example according to circleStatsItemBox orange at my sample) 我希望页脚和页眉的颜色应根据新的div类进行排列(例如,根据我的示例中的circleStatsItemBox橙色)

Thanks 谢谢

try with toggleClass in jquery. 尝试在jQuery中使用toggleClass

ToggleClass : Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument. ToggleClass :从匹配的元素集中的每个元素中添加或删除一个或多个类,具体取决于类的存在或switch参数的值。

$('#ID1.circleStatsItemBox').toggleClass('orange green');

尝试删除后再添加:

$('#ID1').removeClass('circleStatsItemBox orange').addClass('circleStatsItemBox green');

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

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