How do you change the style of a div programmatica

2020-07-05 06:43发布

How do I change the style (color) of a div such as the following?

"<div id=foo class="ed" style="display: <%= ((foo.isTrue) ? string.Empty : "none") %>">
                        <%= ((foo.isTrue) ? foo.Name: "false foo") %>"`

8条回答
狗以群分
2楼-- · 2020-07-05 07:41

It looks like you are writing ASP, or maybe JSP. I'm not too familiar with either language, but the principles are the same no matter what language you are working in.

If you are working with a limited number of colours, then the usual option is to create a number of classes and write rule-sets for them in your stylesheet:


.important { background: red; }
.todo { background: blue; }

And so on.

Then have your server side script generate the HTML to make the CSS match:


<div class="important">

You should, of course, ensure that the information is available through means other than colour as well.

If the colours are determined at run time, then you can generate style attributes:


<div style="background-color: red;">
查看更多
不美不萌又怎样
3楼-- · 2020-07-05 07:44

You should set your colors in CSS, and then change the CSS class programatically. For example:

(CSS)

div.Error {
  color:red;
}

(ASP.NET/VB)

<div class='<%=Iif(HasError, "Error", "")%>'> .... </div>
查看更多
登录 后发表回答