Thymeleaf 动态ID定义指南 - 使用th:id属性
在Thymeleaf中,可以使用Thymeleaf的内置属性th:id来定义动态id。th:id属性可以接受Thymeleaf表达式作为值,从而实现动态id的定义。\n\n例如,假设有一个列表需要渲染,并为每个列表项定义动态id。可以使用Thymeleaf的循环迭代器th:each来遍历列表,并在th:each中使用th:id属性来定义动态id。\n\nhtml\n<ul>\n <li th:each="item : ${items}" th:id="'item-' + ${item.id}">\n Item: <span th:text="${item.name}"></span>\n </li>\n</ul>\n\n\n在上面的例子中,${items}是一个包含列表项的集合,${item}是循环迭代器,${item.id}是每个列表项的id属性的值。通过使用th:id="'item-' + ${item.id}",可以将动态id定义为item-1、item-2、item-3等。\n\n注意,在Thymeleaf中,动态id的定义需要使用单引号将字符串常量括起来,以确保Thymeleaf能够正确解析表达式。
原文地址: https://www.cveoy.top/t/topic/pwOU 著作权归作者所有。请勿转载和采集!