今天遇到了一个th:onclick调用JS方法传参的问题,整理一下分享给大家。
我的代码是这样写的
<span th:onclick="'javascript:fun(\''+${newsInformation.herf}+'\')'">查看详情</span >
然后就会报错,之前经常用th:onclick传id,都可以正常运行,所以这个错误也百思不解。
看一下错误日志:
org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. (template: "admin/newsInformation/list" - line 81, col 48)
翻译一下上述日志,意思大概就是是这种传参方式只能识别数字或者布尔值