【问题标题】:How can I apply style to a div based on condition in thymeleaf?如何根据百里香中的条件将样式应用于 div?
【发布时间】:2016-08-26 11:42:51
【问题描述】:

我有一个<div> 块,我需要根据条件将其设置为display:nonedisplay:block。 html看起来是这样的,

<div style="display:none;"> 
    //some html block content
</div>

我在 thymeleaf 中尝试过以下代码,

<div th:style="${condition} == 'MATCH' ? display:block : display:none"> 
    //some html block content
</div>

但是上面的表达式不起作用。抛出 org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: 错误消息。

我可以通过 th:classappend 设置一些类并使其工作,但想知道 elvis/三元运算符是否支持 thymeleaf th:style 标签。

【问题讨论】:

    标签: thymeleaf conditional-operator


    【解决方案1】:

    在发布问题时解决了它,

    th:style="${condition ? 'display:block' : 'display:none'}" >
    

    会产生必要的条件样式。如果条件为真,则显示设置为阻塞,如果条件为假,则无。

    对于管理员,

    th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
    

    样式设置为display:block,其他角色不显示该块。

    【讨论】:

    • 更正:th:style="${condition ? 'style1' : 'style2'}"
    • 如果您想保留现有样式,可以使用th:styleappend
    【解决方案2】:

    简单的可以写成

    <div th:style="${filed==null} ? 'opacity:.3'">
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多