Why cannot parameters be passed in using global variables (var i = 0), but can be passed in with regional variables (let i = 0)
for (var i = 0; i < inputs.length; i++) {
inputs[i].onfocus = function(){
showTip(i);
};
};
function showTip(n){
tips[n].style.color = "red";
}
For Nikkei questions, please search for:
Closures
,JavaScript block-level scope
let is only valid in the current block-level scope (within the braces of for), and each loop can be regarded as a new variable.
You can refer to the let command in Ruan Yifeng’s Getting Started with ECMAScript 6