jQuery中each方法的使用详解

概述:

  each() 方法规定为每个匹配元素规定运行的函数

  返回 false 可用于及早停止循环,相当于break。

  返回 true 可以结束本次循环,相当于continue。

语法:

rush:js;"> $(selector).each(function(index,element){ })     index - 选择器的 index 位置     element - 当前的元素(也可使用 "this" 选择器)   $(selector).each(function(){ })   $.each(array,function(Key,Value){ })

1.遍历js数组

rush:js;"> $(function(){ var array=["aaa","bbb","ccc"]; $.each(array,function(i,j){ alert(i+":"+j);  //i表示索引,j代表值 }); })

2.遍历Object对象

rush:js;"> var obj = new Object(); obj.name="zs"; $.each(obj,function(name,value) {   alert(this); //this指向当前属性的值,等价于value   alert(name); //name表示Object当前属性名称   alert(value); //value表示Object当前属性的值 });

3.遍历JSON对象

rush:js;"> var json ={"name":"zhangSan","role":"student"}; $.each(json,function(key,value){ alert(key+":"+value); });

4.遍历由多个JSON对象组成的数组

rush:js;"> var json =[{"name":"Amy","role":"student"},{"name":"Tom","role":"student"}]; $.each(json,function(index,value) { alert("index="+index+"\n" +"name:"+value.name+"\n"+"role:"+value.role+"\n"); });

5.遍历jQuery对象

 

rush:js;">    遍历jQuery对象
  • first
  • second
  • 总结

    以上所述是小编给大家介绍的jQuery中each方法的使用详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

    相关文章

    页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
    jQuery实时显示日期、时间 html: <span id=&quot...
    jQuery 添加水印 <script src="../../../.....
    中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
    1. 用Response.Write方法 代码如下: Response.Write(&q...
    Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...