for (i = 0;i < cnt;i++) {
var thumb=thumbnails[i];
var id=thumb.id;
thumb.addEventListener('click', function () {
location.href="SomeWebPage.html?id=" + id;
}, false);
}
The code above results in all thumbnails having the same last id
assignment instead of individual thumbnail id
assignments based on the individual div
id
's. My expectation was that each thumbnail div
would be assigned an event handler that referenced their specific id
in the location.href
url being defined. Instead, all of the event handlers have the same url that references just the last dev
id
.
Use let instead of var
let id=thumb.id;
var is global stuff, thats why only last value is remained in arrow functions memory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.