[英]How can I apply style to a div based on condition in thymeleaf?
I have a <div>
block which I need to set to display:none
or display:block
based on the condition.我有一个
<div>
块,我需要根据条件将其设置为display:none
或display:block
。 The html looks like this, html看起来像这样,
<div style="display:none;">
//some html block content
</div>
I've tried the following code in thymeleaf,我在百里香叶中尝试了以下代码,
<div th:style="${condition} == 'MATCH' ? display:block : display:none">
//some html block content
</div>
But the above expression is not working.但是上面的表达式不起作用。 throws
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:
error message.抛出
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:
错误消息。
I can do th:classappend
to set some class and make this work but want to know if elvis/ternary operator will support on thymeleaf th:style
tag.我可以做
th:classappend
来设置一些类并完成这项工作,但想知道 elvis/三元运算符是否支持 thymeleaf th:style
标签。
Solved it while posting the question,在发布问题时解决了它,
th:style="${condition ? 'display:block' : 'display:none'}" >
would produce the necessary conditional style.将产生必要的条件风格。 If condition is true display is set to block and none if condition is false.
如果条件为真,则显示设置为阻止,如果条件为假,则不显示。
For admin,对于管理员,
th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
the style is set to display:block
and for other roles the block is not displayed.样式设置为
display:block
并且对于其他角色不显示块。
在简单的情况下,它可以写成
<div th:style="${filed==null} ? 'opacity:.3'">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.