繁体   English   中英

Javascript,onclick,未捕获ReferenceError:未定义FAVORITES

[英]Javascript, onclick, Uncaught ReferenceError: FAVOURITES is not defined

我有一个Uncaught ReferenceError: FAVOURITES is not defined(onclick) ,我不知道button.setAttribute("onclick", "ContactLoader.table(" + table +")"); sintax错误在哪里button.setAttribute("onclick", "ContactLoader.table(" + table +")");

 ContactLoader.table = function(table){ ContactLoader.CURRENT_PATTERN = null; ContactLoader.CURRENT_TABLE = table; ContactLoader.CURRENT_LETTER = "A"; ContactLoader.loadData(); } function generateSearchLetter(){ var divSearch = document.getElementById("search"); if(divSearch.lastChild.id === "search_name"); divSearch.removeChild(divSearch.lastChild); var divSearchLetter = document.createElement('div'); divSearchLetter.setAttribute("id", "search_letter"); divSearch.appendChild(divSearchLetter); var divAllFav = document.createElement('div'); divAllFav.setAttribute("class", "search_all_favourites"); divSearchLetter.appendChild(divAllFav); var arr1 = new Array("ALL","FAVOURITES"); for(var i=0; i<2; i++){ var button = document.createElement('div'); var textNode = document.createTextNode(arr1[i]); button.appendChild(textNode); var table = button.textContent; button.setAttribute("class" , "letterAF"); button.setAttribute("onclick", "ContactLoader.table(" + table +")"); divAllFav.appendChild(button); } } 

当您尝试将event绑定到DOM element ,使用setAttribute绑定click事件不是一个好主意。 从很多年前开始就不推荐使用这种方法。 要将click事件bindbutton变量,最好替换为:

button.setAttribute("onclick", "ContactLoader.table(" + table +")"); 

button.addEventListener("click", function () {
   ContactLoader.table(table);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM