> 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/17-jie-ou-de-mo-ban-luo-ji/17.3-the-th-ref-attribute.md).

# 17.3 th:ref属性

`th:ref`仅仅是一个标记属性。从处理的角度来看，它什么也没有做，只是在模板被处理时消失了而已。但是，它的用处体现在它会像标记引用一样工作。比如，它可以通过标记选择器的名字解析，就像一个标签名字或一段标记（`th:fragment`）。

如果我们有如下的选择器：

```markup
  <attr sel="whatever" .../>
```

这会匹配：

* 任何`<whatever>`标签
* 任何带`th:fragment="whatever"`属性的标签
* 任何带`th:ref="whatever"`属性的标签

`th:ref`的优势是什么呢？比如，使用纯HTML的`id`属性？我们可能是仅仅不想往标签里加这么多`id`和`class`属性作为逻辑锚，来污染我们的输出。

在同样意义上，`th:ref`的劣势是什么呢？显而易见，我们得往我们的模板里添加点Thymeleaf逻辑。

注意，`th:ref`属性`不仅仅可以用于解耦的逻辑模板文件`：它在其它场景下同样有效，比如片段表达式（`~{...}`）。
