[英]thymeleaf:external js file
我试图在thymeleaf项目中使用外部javascript文件,所以我做了以下工作:
这就是文件的声明方式(我将其放在/ body之前,就像其他许多帖子中所建议的那样)
<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”。
我也尝试过
function change2() {
document.getElementById("l1").className="";
document.getElementById("l2").className="selected";
};
并且我收到“未捕获的TypeError:无法将属性'className'设置为null”
似乎js文件甚至没有被处理。任何解决方案?
提前致谢
我建议您使用事件处理程序代替对href属性的函数调用。 因此,您可以将锚链接更改为此:
<a id="l2" href="javascript:void(0);">l2_Link</a>
要添加click事件,您必须使用Rooster建议的window.onload事件。
window.onload = function (){
document.getElementById ("l2").addEventListener ("click", change2, false);
}
您可以在以下位置查看其工作示例: http : //jsfiddle.net/RKSZ2/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.