Ich habe versucht, eine for-Schleife zu verwenden, um divs dynamisch zu aktualisieren, aber es scheint ein Problem zu sein. Das erste Mal, dass ich es laufen läuft gut und Chrom-Protokolle ...Dynamisch Aktualisieren von divs mit für LOOP
gallery.js:9 class length = 1
gallery.js:11 testing uniqueId-> product_1
gallery.js:13 adding uniqueID product_1 to class
gallery.js:15 j is -->0
gallery.js:17 updated n.o of images in the class to 1
aber das zweite Mal, dass ich laufen sie etwas schief geht ...
gallery.js:9 class length = 2
gallery.js:11 testing uniqueId-> product_2
gallery.js:13 adding uniqueID product_2 to class
gallery.js:15 j is -->0
gallery.js:13 adding uniqueID product_2 to class
gallery.js:15 j is -->1
gallery.js:17 updated n.o of images in the class to 2
Wie Sie Linie 13-15 Wiederholung sehen und irgendwie, dass Namen alle divs die gleichen, zum Beispiel von product_0 product_1 ... etc ..
Heres der Code:
var clss = document.getElementsByClassName('thumbnail');
var clssLength = clss.length;
console.log('class length = ' + clssLength);
var uniqueId = "product_" + clssLength;
console.log('testing uniqueId-> ' + uniqueId);
for (var j = 0; j < clss.length; j++) {
clss[j].setAttribute('id', uniqueId);
console.log('j is -->' + j);
}
Vielen Dank im Voraus
Sie mehr erklären könnte? Ich verstehe nicht, was daran falsch ist. – RobertAKARobin
ohhhh, ich brauchte die for-Schleife nicht –
Ich könnte das stattdessen getan haben: –