javascript - 如何将百里香中的javascript外部化为片段?
问题描述
我想创建一个可重用的 javascript 片段,然后我可以简单地包含在<th:block layout:fragment="script">
块中,如下所示:
<th:block layout:fragment="script">
<div th:replace="test :: script"></div>
</th:block>
与test/script.html
:
<html xmlns="http://www.w3.org/1999/html" xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="script">
<script type="text/javascript">
$(document).ready(function(){
alert('test alert');
});
</script>
</div>
</body>
</html>
当我直接使用js
in 代替th:replace
语句时,警报会执行。当我尝试包含上面写的片段时,什么也没有发生。
问题:我怎么知道thymeleaf
包含外部化的js?
解决方案
我不得不将外部替换<div>
为<script>
:
<th:block layout:fragment="script"> < script th:replace="external :: script"> </th:block>
并且:
<脚本th:fragment="脚本">
推荐阅读
- c# - C# JSON 匿名类型 - 具有相同名称的多个属性
- git - Git CLI 新项目无法成功运行
- python - 有没有一种方法可以从法语句子中提取名词-形容词对?
- linux - 通过控制台在远程服务器上测试 Kafka 代理/主题的最佳简便方法是什么?
- javascript - 使用 JavaScript 将 AODB 中的绝对路径更改为相对路径
- c - 打印出 MATH_PI 打印出一个随机浮点值
- testing - 量角器 7 中带有异步等待的短堆栈跟踪
- javascript - jQuery中的动态乘法
- python - 具有稳定值的 Matplotlib 颜色映射映射
- java - 过滤并返回结果 Springboot、Spring Jpa、Hibernate Thymeleaf