jQuery关于遍历数组的问题 [英] jQuery question about looping through an array

查看:63
本文介绍了jQuery关于遍历数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组:

I have an array like this:

var gridArray = [ 1,2,0,1,2,3,2,1,2,3,4,4,3,2,2,2,0 ]

我想要这样的jQuery each()函数:

And I would like a jQuery each() function like this:

$('li.node').each(function() {
                loop through the array
                            var rndTile = the current array digit

                $(this).css({ 'background-image': 'url(images/'+arrayDigit+'.jpg' });
            });

我该如何实现呢?

推荐答案

您需要将参数传递给每个回调.

You need to pass parameters to your each callback.

$(gridArray).each(function (i, val) {
  // i is the index in this loop.
  // val is the value at gridArray[i] in this loop.
});

这篇关于jQuery关于遍历数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆