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为变量?
谢谢
你现在这样遍历没有问题呀,这样可以传一个变量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))
}