let homeTravelItemImg = document.getElementsByClassName("item-img");
let homeTravelItemIcon = document.getElementsByClassName("item-icon");
for (var i = 0; i < homeTravelItemIcon.length; iPP) {
homeTravelItemIcon[i].onclick = function() {
homeTravelItemImg[i].style.opacity = "1";
homeTravelItemIcon[i].style.display = "none";
}
}
The main thing the code implements is that when I click on an icon, the icon disappears and the opacity of the background image changes;
when I trigger the click event, the console reports an error:
Uncaught TypeError: Cannot read property "style" of undefined
and I will not report an error when I change the var of the for loop to let. Why?
is it because of the block-level scope?