【问题标题】:Thymeleaf - get dom element attributesThymeleaf - 获取 dom 元素属性
【发布时间】:2018-03-30 08:07:40
【问题描述】:

我使用 Thymeleaf 创建 html 组件。组件在单独的文件中声明:

buttons.html中基本按钮的声明

<div th:fragment="btn-basic" class="btn btn-basic" th:text="${text}" th:classappend="${class}">
    Button
</div>

这个想法是为组件提供某种类型的工具集。使用这个组件的代码是:

<div th:replace="~{buttons :: btn-basic (text='Button Text', class='button-class')}"></div>

它运行良好,但我考虑按钮需要具有以下属性的情况:onclick="..."data-customAttr="..." 或任何其他属性。问题来了:

  • 如何将属性传递给按钮?
  • 一种方法是作为fragment的参数传递,但是太丑了。
  • 有没有办法在片段中获取占位符的属性? (见下面的例子)

我想这样称呼片段:

<div th:replace="~{buttons :: btn-basic (text='Button Text', class='button-class')}" onclick="..." data-customAttr="..."></div>

并且在btn-basic 片段中想要获取这些属性并附加到它。像这样的:

<div th:fragment="btn-basic" class="btn btn-basic" th:text="${text}" th:classappend="${class}" onclick="..." data-customAttr="...">
    Button
</div>

有什么想法吗?

【问题讨论】:

    标签: html thymeleaf


    【解决方案1】:

    我也有类似的想法,但问题是,如果一个组件的定制和结果一样复杂,那有什么好处呢?

    顺便说一句。使用Thymeleaf Layout Dialect,您可以执行以下操作:https://ultraq.github.io/thymeleaf-layout-dialect/Examples.html#reusable-templates,我赞成这样做,而不是一切作为参数的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-28
      • 2012-04-14
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多