如何添加<a> into </a> <li> <a> using thymeleaf each loop

时间:2017-11-28 21:28:13

标签: html thymeleaf

I have a todo list and I want to show each item in a "li" tag. And in this tag, i also want to add a link X to be able to delete the item. But I got an error:

org.xml.sax.SAXParseException: The value of attribute "th:text" associated with an element type "li" must not contain the '<' character.

Here is the code that got the error:

<li th:each="todoitem : ${todolist}" th:text="${todoitem.text} + <a href='#' class='close' aria-hidden='true'>&times;</a>" th:attr="data-value=${todoitem.id}" ></li>

I also tried like this, also did not work:

<li th:each="todoitem : ${todolist}" th:text="${todoitem.text}" th:attr="data-value=${todoitem.id}"><a href='#' class='close' aria-hidden='true'>&times;</a></li>

The code that I am trying to generate is like this:

<li data-value="0">text of todo list <a href="#" class="close" aria-hidden="true">×</a></li>

So how can I make a loop that can also add the link into the li tag?

1 个答案:

答案 0 :(得分:1)

一种选择是使用<span>呈现文本。

从第二次尝试开始,即

<li th:each="todoitem : ${todolist}" th:text="${todoitem.text}" th:attr="data-value=${todoitem.id}"><a href='#' class='close' aria-hidden='true'>&times;</a></li>

th:text移至新的<span>

<li th:each="todoitem : ${todolist}" th:attr="data-value=${todoitem.id}">
    <span th:text="${todoitem.text}" th:remove="tag"></span>
    <a href='#' class='close' aria-hidden='true'>&times;</a>
</li>

您也可以使用th:inline="text" as explained here