簡體   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