如何使用thymeleaf为html元素分配多个属性

Djo*_*vic 21 html spring-mvc thymeleaf

我的百里香页面上有一个选择框.我已经为它定义了一个属性,如:

th:attr="labelId='associateTSF' + ${mViewStat.index}"
Run Code Online (Sandbox Code Playgroud)

有没有办法设置多个?就像是:

th:attr="labelId='associateTSF' + ${mViewStat.index}; missionGroup=${mView.missionGroup}"
Run Code Online (Sandbox Code Playgroud)

我已经试过了这个; 而且空白,没有成功.我发现的所有例子都是单值.

谢谢!

Djo*_*vic 38

找到了!它有效.它应该用逗号分隔.

HTML禁止重复属性,因此代码不正确.但是,th:attr和data-th-attr允许您指定由逗号分隔的多个属性,例如:

 <a href="#"     data-th-attr="data-groupid=${somevalue},
                  data-groupname=${someothervalue}">...</a>
Run Code Online (Sandbox Code Playgroud)

在这次讨论中找到了它:https: //github.com/thymeleaf/thymeleaf/issues/93

  • 甜蜜的,斯瓦兹 - 非常感谢 (2认同)

utk*_*mez 15

这对我有用

th:attr="attrParam1=${attrVal1}, 
         width=${width != null ? width : null}, 
         height=${height != null ? height : null}"
Run Code Online (Sandbox Code Playgroud)