gpt4 book ai didi

JavaScript,onclick, Uncaught ReferenceError : FAVOURITES is not defined

转载 作者:行者123 更新时间:2023-12-03 07:42:40 25 4
gpt4 key购买 nike

我有一个未捕获的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com