function $id(id) {return document.getElementById(id);}; for(x = 1;x < 4;x++) { $id("button_img_"+x).addEventListener("click",slide_button); }如上述代码,id需要被遍历,如何让document.getElementById("id")里的id为变量?谢谢
1 回答

小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
你现在这样遍历没有问题呀,这样可以传一个变量id进去。
function $id(id) {
return document.getElementById(id);
};
for (x = 1; x < 4; x++) {
(function(x){
$id("button_img_" + x).addEventListener("click", function() {
alert(x);
});
}(x))
}
添加回答
举报
0/150
提交
取消