首页 > 解决方案 > thymeleaf 片段中的 $(document).ready 函数 - $ 未定义

问题描述

我正在尝试将带有 $(document).ready 函数的 js 文件添加到 thymeleaf 片段中,以便在包含片段时始终添加 js 文件。当我这样做时,我在浏览器中收到错误消息“Uncaught ReferenceError: $ is not defined”。

pageIncludingFragment.html

...
<div th:include="fragment :: fragment"></div>
...

片段.html

<div th:fragment="fragment">
   ...
   <script th:src="resources/js/fragment.js"></script>
</div>

片段.js

$(document).ready(function() {
   ...
});

有没有办法做到这一点,或者我必须每次都直接将脚本包含在 pageIncludingFragment.html 中?

标签: javascripthtmljquerythymeleaf

解决方案


freen-m是对的,我的 jquery 包含在片段的包含之下。


推荐阅读