Passing values to onclick [duplicate]
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . If I create a whole lot of HTML elements using a loop, like for (i= 1; i < 100; i++) { var my_element = document.createElement ("td"); row.appendChild (my_element); my_element.onclick = function () {my_function (i)); } then when the element is clicked, the value of i passed to my_function is always 100, regardless of what number element is calling it. I have