我想要使用一个外部JavaScript文件中thymeleaf项目,所以我做了以下内容:thymeleaf:外部JS文件
这是文件的声明方式(我把此之前/身体在许多其他职位)建议
<script type="text/javascript" th:src="@{/resources/lor.js}"></script>
这是HTML的函数调用
<a id="l2" th:href="'javascript:change2();'">
,这是JS文件
function change1() {
document.getElementById("l1").setAttribute("class", "selected");
document.getElementById("l2").setAttribute("class", "");
};
function change2() {
document.getElementById("l1").setAttribute("class", "");
document.getElementById("l2").setAttribute("class", "selected");
};
但是,我从萤火虫中收到以下错误“Uncaught ReferenceError:change2 is not defined”。
我也试过
function change2() {
document.getElementById("l1").className="";
document.getElementById("l2").className="selected";
};
和我得到“遗漏的类型错误:不能为空的设置属性‘的className’”
好像js文件甚至没有processed.any解?
在此先感谢
好像change2功能在外部脚本之前烧制中完全负载。你可以使用onload处理程序来调用change2来修复它。看到这个:http://stackoverflow.com/questions/3842614/how-do-i-call-a-javascript-function-on-page-load – Rooster