二十八、each()方法

二十八、each()方法

28.1 第一种使用方式:对象.each(funvtion(){})

对象.each(funvtion(){}) 接受一个参数就是匿名函数

1
2
3
4
// 使用each遍历
$("li").each(function(i){
    console.log(i);
});

UZNztO.png

28.2 第二种使用方式: $.each(target,fn)

  • $.each(target,fn)

    • 一般用于遍历数组或者遍历对象的时候

    • target:要遍历的目标

    • fn:执行的函数

    • 函数中有2个参数:

      • 第一个参数:索引值

      • 第二个参数:成员值

1
2
3
4
5
6
// 定义一个数组
var arr = ["a","b","c","d"];
// 使用each方法的第二种方式
$.each(arr, function(index, value){
    console.log(index, value);
});

UZN0Tf.png

1
2
3
4
5
6
7
8
9
10
// 定义一个对象
var obj = {
    a: "1",
    b: "2",
    c: "3"
}
// 遍历对象
$.each(obj, function(key, value){
    console.log(key, value);
});

UZNclj.png

点击查看

本文标题:二十八、each()方法

文章作者:Mango

发布时间:2020年07月08日 - 22:30:38

最后更新:2020年07月08日 - 22:35:39

原始链接:https://mango185.github.io/post/dd22da61.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------