Javascript,onclick,Uncaught ReferenceError:未定义FAVORITES

时间:2016-02-11 17:34:58

标签: javascript onclick referenceerror

我有Uncaught ReferenceError: FAVOURITES is not defined(onclick),我不明白button.setAttribute("onclick", "ContactLoader.table(" + table +")");中我的sintax错误在哪里。



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);	
	}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

当您尝试将event绑定到DOM element 时, 最好使用{{绑定点击事件 1}}。多年以前,这种方法已被弃用。 要将setAttribute事件bind移至click变量,最好更换:

button

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