开发者

how to add a number onto a variable name and cycle upward: x1 x2 x3 etc in jQuery

I need to change a variable name each time a piece of code loops, and increment it by one.

I'm not sure how to append it onto the variable name, so in my example each time it loops ( once for each div that's a direct child of body ) it assigns a new variable name --> myFunction1 myFunction2 myFunction3 and so-on.

Here's what i have:

            $(document).ready(function() {
                $('body > div').each(function(i) {
            开发者_如何学Go        alert(i);
                    // for each carousel
                    function myFunction+i(carousel) {
                        // whatever
                        })
                    };
                    });
            });


Not exactly sure why you would do that, but it can be done like this:

window["myFunction"+i] = function(){...};

assuming you want these functions to be global. Then it can be called as normal:

myFunction1();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜