我有一堆不同的按鈕(不同的ID),點擊後,執行相同的邏輯。這就是爲什麼我在click
上添加了聽衆的原因,clearOrSelectAll
函數以適當的clearSelectArr[i]
作爲輸入來執行。添加偵聽器按鈕來執行功能
目前我正在此爲我的錯誤:
Uncaught TypeError: undefined is not a function
所有的按鈕有一個class="clearSelect"
我的代碼:
var clearSelectArr = [ "selectAllXP", "selectAll7", "selectAll8",
"selectAll8_1", "selectAll10_6", "selectAll10_8",
"selectAll10_9", "selectAllios", "selectAll4_4",
"selectAll4_3", "selectAll4_2", "selectAll4_1",
"selectAll4_0", "clearAllXP", "clearAll7",
"clearAll8", "clearAll8_1", "clearAll10_6",
"clearAll10_8", "clearAll10_9", "clearAllios",
"clearAll4_4", "clearAll4_3", "clearAll4_2",
"clearAll4_1", "clearAll4_0"];
var buttons = document.getElementsByClassName("clearSelect");
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.addEventListeners("click", clearOrSelectAll(button.id));
}
每個按鈕都有一個與之關聯的ID 。這些ID與clearSelectArr
中的ID相同。我希望能夠添加與按鈕相同的ID的偵聽器功能。
該方法被稱爲'addEventListener',而不是'addEventListeners'。 – 2014-10-28 22:56:09
費利克斯可能解決了你的問題,但在旁邊注意:'clearSelectArr [clearSelectArr.indexOf(button.id)])'與'button.id'一樣... – Jonathan 2014-10-28 23:03:37
@Jonathan darn .. – Liondancer 2014-10-28 23:07:38