Javascript,onclick,Uncaught ReferenceError:未定义FAVORITES

时间:2022-11-05 13:57:45

I have an Uncaught ReferenceError: FAVOURITES is not defined(onclick), i don't understand where is my sintax error in button.setAttribute("onclick", "ContactLoader.table(" + table +")");.

我有一个Uncaught ReferenceError:FAVORITES没有定义(onclick),我不明白我的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);	
	}
}

1 个解决方案

#1


0  

As you're trying to bind an event to a DOM element, it's not a good idea to bind click event using setAttribute. This approach is been deprecated since many years ago. To bind the click event to the button variable, it is better to replace:

当您尝试将事件绑定到DOM元素时,使用setAttribute绑定click事件不是一个好主意。这种方法自多年前就已被弃用。要将click事件绑定到按钮变量,最好替换:

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

with

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

#1


0  

As you're trying to bind an event to a DOM element, it's not a good idea to bind click event using setAttribute. This approach is been deprecated since many years ago. To bind the click event to the button variable, it is better to replace:

当您尝试将事件绑定到DOM元素时,使用setAttribute绑定click事件不是一个好主意。这种方法自多年前就已被弃用。要将click事件绑定到按钮变量,最好替换:

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

with

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