jQuery-each()方法

1、$(selector).each(function(index, element) {})方法遍历一个jquery对象,为每一个匹配的元素运行定义的函数。

index: 选择器的index位置。

element: 当前的元素。

2、$.each()用于迭代数组和对象。数组和类似数组的对象通过一个长度属性来迭代数字索引,从0到length-1。其他对象通过其属性名进行迭代。

$.each(array, function(index, value) {})
数组的回调函数每次传递一个索引和相应的数组值作为参数。
对象的回调函数每次传递一个键值对。
如果要终止迭代返回false;
返回非false相当于continue跳出当前迭代,转到下一个迭代。

$(function() {
    $("#table2 tr td").each(function(index, element) {
        $(this).mouseover(function() {
            alert(index);
            alert($(element).html());
            $(this).css({'background': '#f00'});
        })
    })
    var arr1 = ['0', '33', '889'];
    $.each(arr1, function(index, value) {
        alert(index +": " + value);
        return (index == 1);
    })
    var obj1 = {
        "name": 'roronoa',
        'age': '25'
    }
    $.each(obj1, function(index, value) {
        alert(index +": " + value);
    })
})
原文地址:https://www.cnblogs.com/wanbi/p/4340905.html