Strange things in JavaScript “for”

后端 未结 6 1425
误落风尘
误落风尘 2020-12-19 05:07

I\'m using jQuery and I have a strange thing that I don\'t understand. I have some code:

for (i = 1; i <= some_number; i++) {
    $(\"#some_button\" + i).         


        
6条回答
  •  余生分开走
    2020-12-19 05:47

    This is very clever code. So clever it's a question on SO. :) I'd sidestep the question altogether by dumbing the code down, just to have a chance at understanding it (or having a colleague understand it) six months from now. Closures have their place, but in this case I'd avoid them in favour of more understandable code.

    Probably, I'd attach the same function to all the buttons, which would get the button from the event, strip "some_button" from the ID, and alert the result. Not nearly as pretty, but I guarantee everyone in the office could follow it at a glance.

提交回复
热议问题