CSS HTML 属性中的 JSTL if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1606435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:10:37  来源:igfitidea点击:

JSTL if-statement inside HTML-attribute

cssjspjstljsp-tags

提问by Mickel

is it possible to do something like this in JSTL:

是否可以在 JSTL 中做这样的事情:

<div class="firstclass<c:if test='true'> someclass</c:if>">
   <p>some other stuff...</p>
</div>

Is there any way to get this to work, or is there a better way to add a class by looking at a JSTL-if-statement?

有没有办法让它工作,或者有没有更好的方法通过查看 JSTL-if-statement 添加一个类?

回答by runfa

It's also possible to use an EL expression directly like this:

也可以像这样直接使用 EL 表达式:

<div class="${booleanExpr ? 'cssClass' : 'otherCssClass'}">
</div>

回答by simon


<c:if test='true'> 
  <c:set value="someclass" var="cssClass"></c:set>
</c:if> 
<div class="${cssClass}">
   <p>some other stuff...</p>
</div>

回答by elciospy

That works for me!

这对我行得通!

<div id="loginById" style="height:<% out.print(instanceClass.booleanMethod()? "250px": "150px");%>">