[英]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.