onClick event in a For loop

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

    Sorry if I did not understand your question properly, From the code I understand that, you are trying to add an onclick handler to all the list elements in found in the game tag (perhaps it should be a class / id).

    The for loop will be executed when the script tag / file loads without any user interaction.

    If you wish to assign a function that uses the current value of the counter. Use the following code:

    itemLists[i].onclick = (function() {
        return function() {
             // TODO ---
            // Your handler code here
    }
    })();
    

提交回复
热议问题