的問題是您的單擊事件處理程序是,在實際功能做的工作(dodaj
)的調用將和功能並沒有接到參考事件的匿名函數 - 匿名功能是。
所以它接收到的事件,您可以更改匿名函數,然後把它傳遞給dodaj
這樣的:
prod[i].addEventListener("click", function(evt) {
dodaj(evt);
}, false);
然而,由於「包裝」的功能實在是不添加到您的代碼的任何值,你可以/應該完全刪除它,只需將實際的回調函數註冊爲click事件處理程序。
var prod = document.querySelectorAll("button");
function dodaj(e) {
console.log(e.target);
}
for(var i = 0; i < prod.length; i++) {
// When any button gets clicked, call the dodaj function
// directly. This is the function that will receive a
// reference to the click event.
prod[i].addEventListener("click", dodaj);
}
<button id="one">Click Me</button>
<button id="two">Click Me</button>
<button id="three">Click Me</button>
<button id="four">Click Me</button>
嘗試:PROD [I] - 閱讀進度 - ( 「點擊」,函數(E){ dodaj(E); },假); – Hobroker