【发布时间】:2015-03-15 05:04:24
【问题描述】:
我有一个百里香片段来创建输入字段,例如:
<div th:fragment="formField">
<input th:type="${type}" th:errorclass="field_error" th:field="*{__${field}__}" th:placeholder="#{__${placeholder}__}" />
</div>
这个片段是例如像这样使用:
<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password')">
现在应该根据片段的参数将自动对焦属性添加或不添加到输入字段。使用片段,例如像这样应该添加自动对焦属性:
<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password', autofocus='autofocus')">
我找不到根据片段参数有条件地将 autofocus 属性添加到输入标记的方法。我尝试使用 th:attr 但总是出现语法错误。
有没有办法用百里香有条件地创建html属性?
【问题讨论】:
-
你试过在html中声明autofocus字段吗(第一个代码sn-p)其他字段都被删除了。另一个建议,您可以尝试 thymeleaf 中的
th:with属性 -
th:with 只允许你定义变量。
-
fixed-value-boolean-attributes 有一些特殊属性,它允许您根据