【发布时间】:2021-05-30 19:35:39
【问题描述】:
if (index === 0) {
const image = document.createElement('img');
image.alt = 'image';
image.src = 'https://media.s-bom.com';
image.width = '100';
li.appendChild(image);
}
if (index === 1) {
const image = document.createElement('img');
image.alt = 'image';
image.src =
'https://images-na.ssl-images-amazon.com';
image.width = '100';
li.appendChild(image);
}
if (index === 2) {
const image = document.createElement('img');
image.alt = 'image';
image.src =
'https://upload.wikimedia.orgmmer.jpg/220px-The_pragmatic_programmer.jpg';
image.width = '100';
li.appendChild(image);
}
嘿,我在 if 条件下有上面的代码,我希望它更清晰而不是重复,所以任何人都知道如何更干净。
【问题讨论】:
-
因为唯一的区别似乎是
src只把它放在if()里面。无需复制其余部分
标签: javascript if-statement dom indexing