如何根据百里香中的条件将样式应用于div? [英] How can I apply style to a div based on condition in thymeleaf?
问题描述
我有一个< div>
块,我需要将其设置为 display:none
或 display:block
根据条件。 html看起来像这样,
I have a <div>
block which I need to set to display:none
or display:block
based on the condition. The html looks like this,
<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>
但以上表达式不起作用。引发 org.thymeleaf.exceptions.TemplateProcessingException:无法解析为表达式:
错误消息。
But the above expression is not working. throws org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:
error message.
我可以做 th:classappend
来设置一些类并使其工作,但是想知道猫王/三元运算符将在thymeleaf th:style
标记上支持。
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:style="${condition ? 'display:block' : 'display:none'}" >
将产生必要的条件样式。如果条件为true,则显示设置为阻止,如果条件为false,则不显示。
would produce the necessary conditional style. If condition is true display is set to block and none if condition is false.
对于管理员,
th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
样式设置为 display:block
对于其他角色,则不显示该阻止。
the style is set to display:block
and for other roles the block is not displayed.
这篇关于如何根据百里香中的条件将样式应用于div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!