thymeleaf js调用
时间: 2023-10-14 18:05:07 浏览: 161
Thymeleaf provides a convenient way to invoke JavaScript code within your web application. To call a JavaScript function from Thymeleaf, you can use the `th:inline` attribute to enable inlining of JavaScript code.
Here's an example of how you can call a JavaScript function using Thymeleaf:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
...
</head>
<body>
...
<script th:inline="javascript">
function myFunction() {
// Your JavaScript code here
console.log("Hello from JavaScript!");
}
</script>
...
<button onclick="myFunction()">Invoke JavaScript</button>
...
</body>
</html>
```
In the above example, the `th:inline="javascript"` attribute enables inline JavaScript code within the `<script>` tag. You can define your JavaScript functions within this block.
To call the `myFunction()` function, you can use the `onclick` event on an HTML element, such as a button. In this case, clicking the "Invoke JavaScript" button would trigger the function and execute your JavaScript code.
Remember to include the necessary Thymeleaf XML namespace (`xmlns:th="http://www.thymeleaf.org"`) in your HTML file's root element for Thymeleaf to process the `th:inline` attribute correctly.
I hope this helps! Let me know if you have any more questions.
阅读全文