jQuery-each()方法:遍历数组、对象、选择集

用法1:遍历数组

var arrayOne=[''a,'b','c'];

1.1:$(arrayOne).each(

function()

{

console.log(this);

//a 

//b

// c

}

function(key,value)

扫描二维码关注公众号,回复: 3013496 查看本文章

{

console.log(key+':'+value);

//0:a

//0:b

//0:c

}

);

1.2:$.each(arrayOne,

function()

{

console.log(this);

}

);

用法2:遍历对象

var objectOne={a:'one',b:'two',c:'three'};

$.each(objectOne,

function()

{

console.log(this);

}

);

注*用遍历数组的第一个方法,遍历对象会失败。

用法3:迭代选择集中的数组

  //对id为RubberSoul的ul中的所有li元素进行迭代,并为其中的偶数项增加样式

   $('ul#RubberSoul li').each(
            function (key)
            {
                if (key & 1)
                {
                    $(this).addClass('rubberSoulEven');
                }
            }
            );

猜你喜欢

转载自blog.csdn.net/xiangwenfly/article/details/51317170
今日推荐