onClick event in a For loop

后端 未结 4 1088
误落风尘
误落风尘 2020-12-01 14:48

I\'ve tried to create a loop with a for, and increment by an onclick event, but it doesn\'t work.

A part of js :

 var gameCase = [\'\', \'\', \'\', \         


        
4条回答
  •  悲哀的现实
    2020-12-01 15:27

    Wrap your listener:

    onclick = (function(i) {return function() {
        ...
    };})(i);
    

    This fixes your variable scope issues.

提交回复
热议问题