onClick event in a For loop

后端 未结 4 1089
误落风尘
误落风尘 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:14

    Another option is to use a forEach loop this creates a new variable for each iteration.

    var gameCase = ['', '', '', '', '', '', '', '', ''], // 9
    itemLists = $('game').getElementsByTagName('li'); // 9 items
    
    itemLists.forEach(function(item,index){
        item.onclick = function() {
          // do something
     }
    });
    

提交回复
热议问题