首页 > 解决方案 > Thymeleaf:循环后删除包含迭代属性(th:each)或评估后包含条件属性(th:if)的div

问题描述

在 Thymeleaf 中,是否有可能delete包含wrapper div条件循环?假设 SpringBoot 应用程序中的以下代码,我打算在评估条件并循环遍历错误列表后擦除第一个和第二个 div。

<div th:if="${errors != null}">
    <div th:each="error : ${errors}">
        <div th:utext="${error}"></div>
    </div>
</div>

我喜欢在不使用 JavaScript 的情况下实现这一点。

标签: htmlspring-bootthymeleaf

解决方案


您也可以使用 a <th:block />(未呈现)。此外,无需将迭代和文本分开。您应该能够像这样生成相同的代码:

<th:block th:if="${errors != null}">
    <div th:each="error : ${errors}" th:utext="${error}" />
</th:block>

(我更喜欢<th:block />th:remove="tag"


推荐阅读