Ich versuche also, viele Bilder automatisch über eine For-Schleife zu drucken. Ich habe drei Listen mit Bildern eines bestimmten Typs: Wald, Menschen und Meer. Ich möchte jedes Bild in jeder Kategorie durchgehen und ausdrucken. Ich habe es stundenlang versucht, aber ich bin ein absoluter Anfänger, also habe ich vielleicht etwas Dummes übersehen. Das ist mein Code bisher:
<script>
const forrest = ["https://cdn.pixabay.com/photo/2020/03/20/13/23/mountain-4950653_1280.jpg", "https://cdn.pixabay.com/photo/2020/01/07/14/18/forrest-4747692_1280.jpg", "https://cdn.pixabay.com/photo/2018/07/30/11/30/landscape-3572374_1280.jpg"]
const people = ["https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg", "https://cdn.pixabay.com/photo/2015/05/15/14/50/concert-768722_1280.jpg", "https://cdn.pixabay.com/photo/2017/08/06/12/06/people-2591874__480.jpg"]
const ocean = ["https://cdn.pixabay.com/photo/2016/11/29/04/19/ocean-1867285_1280.jpg", "https://cdn.pixabay.com/photo/2018/06/13/18/20/wave-3473335__340.jpg", "https://cdn.pixabay.com/photo/2016/12/17/14/33/wave-1913559__340.jpg"]
const type = [forrest, people, ocean]
function myFunction() {
for (var i = 0; i < type.length; i++); {
var list = type[i];
for (var pictures = 0; pictures <= list.length; pictures++); {
var img = document.createElement("img");
img.src = list[pictures];
document.body.appendChild(img);
}
}
}
</script>
Ich habe einige Beispielfotos in den obigen Code eingefügt. Meine Fotos sind auch aus dem Internet. Der Code druckt das erste Foto in der ersten Liste, aber kein anderes Foto.
Sie haben Semikolons nach Ihren for
Schleifenbedingungen platziert:
for (var i = 0; i < some_list.length; i++) /* NO SEMICOLON HERE*/ {
//body
}
Entfernen Sie sie und es funktioniert:
const forrest = ["https://cdn.pixabay.com/photo/2020/03/20/13/23/mountain-4950653_1280.jpg", "https://cdn.pixabay.com/photo/2020/01/07/14/18/forrest-4747692_1280.jpg", "https://cdn.pixabay.com/photo/2018/07/30/11/30/landscape-3572374_1280.jpg"]
const people = ["https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg", "https://cdn.pixabay.com/photo/2015/05/15/14/50/concert-768722_1280.jpg", "https://cdn.pixabay.com/photo/2017/08/06/12/06/people-2591874__480.jpg"]
const ocean = ["https://cdn.pixabay.com/photo/2016/11/29/04/19/ocean-1867285_1280.jpg", "https://cdn.pixabay.com/photo/2018/06/13/18/20/wave-3473335__340.jpg", "https://cdn.pixabay.com/photo/2016/12/17/14/33/wave-1913559__340.jpg"]
const type = [forrest, people, ocean]
function myFunction() {
for (var i = 0; i < type.length; i++) {
var list = type[i];
for (var pictures = 0; pictures < list.length; pictures++) {
var img = document.createElement("img");
img.src = list[pictures];
document.body.appendChild(img);
}
}
}
debugger;
myFunction();
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen