我有一个未捕获的ReferenceError:FAVORITES未定义(onclick)
,我不明白button.setAttribute(“onclick”,“ContactLoader.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);
}
}
最佳答案
当您尝试将事件
绑定(bind)到DOM元素
时,这不是一个好主意使用setAttribute
绑定(bind)点击事件。这种方法自多年前就已被弃用。
要将 click
事件绑定(bind)
到 button
变量,最好替换为:
button.setAttribute("onclick", "ContactLoader.table(" + table +")");
与
button.addEventListener("click", function () {
ContactLoader.table(table);
});
关于JavaScript,onclick, Uncaught ReferenceError : FAVOURITES is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346211/