Thymeleaf:如何使用条件来dynamic添加/删除一个CSS类
通过使用Thymeleaf作为模板引擎,是否可以使用th:if子句dynamic地向/从简单div添加/删除CSS类? 
通常情况下,我可以使用条件子句如下:
 <a href="lorem-ipsum.html" th:if="${condition}">Lorem Ipsum</a> 
我们将创build一个链接到lorem ipsum页面,但只有条件子句为true。
我正在寻找不同的东西:我希望块总是可见的,但根据情况可变的类。
 还有th:classappend 。 
 <a href="" class="baseclass" th:classappend="${isAdmin} ? adminclass : userclass"></a> 
 如果isAdmin是true ,那么这将导致: 
 <a href="" class="baseclass adminclass"></a> 
 是的,可以根据情况dynamic更改CSS类,但不能使用th:if 。 这是由elvis操作员完成的。 
 <a href="lorem-ipsum.html" th:class="${isAdmin}? adminclass : userclass">Lorem Ipsum</a> 
为此目的,如果我没有布尔variables我使用以下内容:
 <li th:class="${#strings.contains(content.language,'CZ')} ? active : ''"> 
另一个非常相似的答案是使用“等于”而不是“包含”。
 <li th:class="${#strings.equals(pageTitle,'How It Works')} ? active : ''"> 
只是为了增加我自己的意见,以防对某人有用。 这是我用的。
 <div th:class="${request.read ? 'mdl-color-text--grey-800 w500' : ''}"> </div> 
th:class的另一个用法与@NewbLeech和@Charles相同,但是如果没有“else”情况,则简化为最大值:
 <input th:class="${#fields.hasErrors('password')} ? formFieldHasError" /> 
如果#fields.hasErrors('password')为false,则不包含类属性。