在JavaScript函数中获取thymeleaf的变量可以通过在html页面中将变量赋值给JavaScript全局变量,然后在JavaScript函数中使用该全局变量来获取thymeleaf的变量值。例如:

在html页面中:

<div th:if="${msg}" id="msg" th:text="${msg}"></div>
<script>
    var msg = [[${msg}]];
</script>

在JavaScript函数中:

function printMsg() {
    var msg = document.getElementById("msg").innerHTML;
    console.log(msg);
}

在html页面中显示thymeleaf的变量值可以使用th:text或th:value属性。例如:

<div th:text="${msg}"></div>
<span th:value="${count}"></span>
在JavaScript函数中和html页面中获取thymeleaf的变量和显示

原文地址: https://www.cveoy.top/t/topic/baA1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录