下面的代码允许我单击图像并在下面生成相同的图像。我试图弄清楚下面的代码如何从处理一个ID到处理两个。我需要能够使用独立ID的图片集点击并在下面生成相同的图片。我似乎无法得到它的工作。单击图像并使用Javascript生成相同的图像
JS:
var imgCount = 5,
i;
for (i = 1; i <= 5; i++) {
document.getElementById('img' + i).addEventListener('click', setImg('img' + i), false);
}
function setImg(id) {
return function() {
var img = document.createElement('img');
if (imgCount) {
imgCount--;
img.src = document.getElementById(id).src;
document.getElementById('footer').appendChild(img);
};
}
}
HTML:下面
<img id="img1" src="http://lorempixel.com/200/100/city/1" />
<img id="img2" src="http://lorempixel.com/200/100/city/2" />
<img id="img3" src="http://lorempixel.com/200/100/city/3" />
<img id="img4" src="http://lorempixel.com/200/100/city/4" />
<img id="img5" src="http://lorempixel.com/200/100/city/5" />
<div id="footer"></div>
为什么不叫过的id的数组功能? – ste2425