Thymeleaf:如何使用条件来动态添加/删除CSS类 [英] Thymeleaf: how to use conditionals to dynamically add/remove a CSS class
本文介绍了Thymeleaf:如何使用条件来动态添加/删除CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Thymeleaf 作为模板引擎,是否可以向简单的 div $ c $动态添加/删除CSS类如果子句
通常情况下,我可以使用如下条件子句: p>
Normally, I could use the conditional clause as follows:
<a href="lorem-ipsum.html" th:if="${condition}">Lorem Ipsum</a>
我们将创建一个指向 lorem ipsum page,但条件子句为true。
We will be creating a link to the lorem ipsum page, but only if condition clause is true.
我正在寻找不同的东西:我想要块总是可见的,但根据具体情况使用可变类。
I'm looking for something different: I'd like the block to always visible, but with changeable classes according to the situation.
推荐答案
还有 th:classappend
。
<a href="" class="baseclass" th:classappend="${isAdmin} ? adminclass : userclass"></a>
如果 isAdmin
是 true
,那么这将导致:
If isAdmin
is true
, then this will result in:
<a href="" class="baseclass adminclass"></a>
这篇关于Thymeleaf:如何使用条件来动态添加/删除CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文