简体   繁体   中英

VB.Net - Change background color (or style) of a DIV

I have a program that checks the status/value of a datapoint and when that datapoint is 0,1,2 it should change the color of a circle (0=green, 1=yellow, 2=red).

My DIV is like this:

 <div class="circle"></div>

with the CSS being:

.circle{
    border-radius:50%;
    width:30px;
    height: 30px;
    background-color: #2aa700;
    float: left;
    margin: 0 20px 0 0;
    }

Now, I have no problem changing the css to a .circlegreen, .circleyellow, .circlered if that makes sense. Just trying to get a handle on how to change that on the fly.

This application has several 'boxes' that are identical in layout (User Controls). I iterate through a database and fill them in. If the 'status' changes, then I want to update the circle color.

Hope that makes sense.

I hope this is what you were looking for:

 $(function() {
     if ($('#hiddenFieldStatus').val() == "0")
     {
         $('.circle').css('background-color', 'green');
     }
 });

Something like this.

You you want to change for only one div means give id to that particular div and use the following code:

<div id="myDiv"></div>

 $(function() {
     //check for some condition if yes execute the following:
     $('#myDiv').css('background-color', 'green');
 }});

Both answers above are correct. I resolved it in the following manner:

Select Case CInt(strStatus)
    Case 1
       divStatus.Attributes.CssStyle.Add("Background-Color","#2aa700")
    Case 2
       divStatus.Attributes.CssStyle.Add("Background-Color","#FF0000")
    Case Is > 2
       divStatus.Attributes.CssStyle.Add("Background-Color","#FF0000")
    Case Else
       divStatus.Attributes.CssStyle.Add("Background-Color","#FFFF00")
End Select

That worked for me. Yes, I think I have an extra statement in there instead of a greater than or equal to, but I am ok with that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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