在使用thymeleaf渲染页面时,遇到如下情况:

1
<button onclick="submit('publish');">提交</button>

submit函数的参数会根据后端参数的不同而动态改变,Google搜索到了一些类似问题,记录一下解决方法。

已经测试可用的:

1
th:onclick="'alert(\'' + ${myVar} + '\');'"

还未测试:

1
2
3
4
5
6
<button th:onclick="'javascript:upload(' + ${gallery} + ')'"></button>

th:onclick="|upload('${command['class'].simpleName}', '${gallery}')|"

<div th:style="'background:url(' + @{/<path-to-image>} + ');'"></div>