我遇到了eventHandler问题。我在这里检查了很多答案,但没有解决,因为eventHandler在以下时间触发:
for(i=0; i < count; i++) {
var n = i+1;
var btn = document.createElement("a");
btn.setAttribute("class", "paginationBtn");
//btn.setAttribute("onClick","setAllData("+n+");setActive(event);");
btn.addEventListener("click", myfunction); //fires automatically
btn.innerHTML = n.toString();
pagination[0].appendChild(btn);
}
然后
for(i=0; i < count; i++) {
var n = i+1;
var btn = document.createElement("a");
btn.setAttribute("class", "paginationBtn");
//btn.setAttribute("onClick","setAllData("+n+");setActive(event);");
btn.addEventListener("click", myfunction()); //fires automatically
btn.innerHTML = n.toString();
pagination[0].appendChild(btn);
}
然后
for(i=0; i < count; i++) {
var n = i+1;
var btn = document.createElement("a");
btn.setAttribute("class", "paginationBtn");
//btn.setAttribute("onClick","setAllData("+n+");setActive(event);");
btn.addEventListener("click", function() {myFunction()}); //fires automatically
btn.innerHTML = n.toString();
pagination[0].appendChild(btn);
}
认为我想添加
之类的功能function(event){
event.target.a.setAttribute("active", "");
anotherFunction();
}
您可以看到我像这样解决了这个问题:
btn.setAttribute("onClick","setAllData("+n+");setActive(event);");
但是我不喜欢它,这不切实际...