> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/thymeleaf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/thymeleaf/i.-using-thymeleaf/5-setting-attribute-values/5.4-appending-and-prepending.md).

# 5.4 附加与前置

Thymeleaf也提供了`th:attrappend`和`th:attrprepend`属性。它们会给既存的属性值附加（后缀）或者前置（前缀）求值的结果。

例如，你可能想要保存一个CSS类的名字，把它加入（不是设置，只是加入）上下文变量里的其中一个按钮。因为哪个特定的CSS类会被使用，取决于用户之前做了什么：

```markup
<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />
```

如果你处理这个模版，把变量`cssStyle`设置成了`"warning"`，你将会得到：

```markup
<input type="button" value="Do it!" class="btn warning" />
```

标准方言里也有两种特定的附加属性：`th:classappend`和`th:styleappend`属性。它们用于给一个元素添加CSS类或样式片段，而不用重写既存的部分：

```markup
<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">
```

（不用担心`th:each`属性。它是一个遍历属性。我们将在之后讨论。）
