You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//error methodfor(vari=0;i<btns.length;i++){btns[i].onclick=function(){console.log(i);/*Click on each button to print out 9, because this is just a definition of a function that is called when the button is clicked, and the cycle is already over: i=9*/}}//method1for(vari=0;i<btns.length;i++){btns[i].index=i;//Save the corresponding I with the attribute of the buttonbtns[i].onclick=function(){console.log(this.index);//What is printed is the corresponding attribute value, equal to the corresponding I//Click the button to call the function i=9, but we don't print I, but the corresponding index value that is stored}}//method2for(vari=0;i<btns.length;i++){(function(num){btns[num].onclick=function(){console.log(num);}})(i)/*The function calls itself (called the anonymous function on the outer layer), and the loop is invoked once at a time. (every call calls I into Num), so printing is the I value that comes out every time.*/}//method3for(vari=0;i<btns.length;i++){btns[i].onclick=(function(num){returnfunction(){console.log(num);}})(i)//The principle is the same, only the function of the call is different}//method4btns=Array.prototype.slice.call(btns);//Convert a pseudo array into a real array //The forEach loop method can be used only by using a real arraybtns.forEach(function(btn,index){//btn is the corresponding button (P tag), and index is the subscript for the P tagbtn.onclick=function(){console.log(index);//Directly with the index parameter, this is the feature of the forEach cycle}})