我試圖將onclick
函數應用於包含三個元素的數組,但設置數組的onclick
屬性不起作用。任何想法都會非常有幫助。 謝謝!嘗試使用onclick函數訪問數組
這裏是我的代碼:
var btnArray = [
document.getElementById('buttonOne'),
document.getElementById('buttonTwo'),
document.getElementById('buttonThree'),
];
console.log(btnArray);
btnArray.onclick = function revilImg() {
console.log('hey');
}
你在哪裏找到,或者知道,當將'document.getElementById'傳遞給map函數時,它需要'document'作爲_thisArg_? – LGSon
@LGSon'document'只是'Document'的一個實例,所以'getElementById'在'Document.prototype'上定義,而不是'document'的屬性。嘗試運行'document.hasOwnProperty('getElementById')',你會發現它是'false'。 – gyre
啊,明白了,謝謝......再加上1 :) – LGSon