替换 Foreach -> For
replace Foreach -> For
我的程序使用 forEach
显示图像。我想用 for
替换它
images.forEach((img, ind) => {
const oImage = new Image();
oImage.onload = () =>
{
ctx.drawImage(oImage, ind * 100, 0, 100, 100);
};
oImage.src = img.textContent;
}
);
}
我不明白你为什么要去掉 forEach,但你有答案而且很简单:
for (let i = 0; i < images.length; i++) {
const oImage = new Image();
oImage.onload = () => {
ctx.drawImage(oImage, i * 100, 0, 100, 100);
};
oImage.src = images[i].textContent;
}
我的程序使用 forEach
显示图像。我想用 for
images.forEach((img, ind) => {
const oImage = new Image();
oImage.onload = () =>
{
ctx.drawImage(oImage, ind * 100, 0, 100, 100);
};
oImage.src = img.textContent;
}
);
}
我不明白你为什么要去掉 forEach,但你有答案而且很简单:
for (let i = 0; i < images.length; i++) {
const oImage = new Image();
oImage.onload = () => {
ctx.drawImage(oImage, i * 100, 0, 100, 100);
};
oImage.src = images[i].textContent;
}