概述:

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

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

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

语法:

  $(selector).each(function(index,element){ })

    index - 选择器的 index 位置
    element - 当前的元素(也可使用 "this" 选择器)

  $(selector).each(function(){  })

  $.each(array,function(Key,Value){ })

1.遍历js数组

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

2.遍历Object对象

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对象

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

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

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对象

  <head>
  <meta charset="utf-8" />
<title>遍历jQuery对象</title>
<script src="js/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(function(){
$("input[type='button']").bind("click",function(){
$("li").each(function(){
alert($(this).text())
});
});
});
</script>
</head>
<body>
<input type="button" value="触发事件"/>
<ul>
<li>first</li>
<li>second</li>
</ul>
</body>

jQuery中有关each方法的使用的更多相关文章

  1. jquery 中一些 特殊方法 的特殊使用 一览表

    cnblogs的页面, 一种是管理页面, 是随笔的列表 a full list of essays. 另一种是 首页. 要搜索文档的话, 就使用 "首页"的那种方式. 一个jque ...

  2. 解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy”错的方法

    解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the ...

  3. jQuery中的join方法

    和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...

  4. jquery中的index方法

    问题描述:灵活使用jquery中的index方法 方法签名:index([selector|element]) 用法概述:P1.index(P2)  //调用者P1可以为对象或集合 参数为空,返回P1 ...

  5. 用JQuery中的Ajax方法获取web service等后台程序中的方法

    用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...

  6. jQuery中的ready方法及实现按需加载css,js

    模拟jQuery中的ready方法及实现按需加载css,js 一.ready函数的实现 经常用jQuery类库或其他类库中的ready方法,有时候想想它们到底是怎么实现的,但是看了一下jQuery中的 ...

  7. $.ajax()方法详解 jquery中的ajax方法

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  8. jQuery中的$.ajax()方法

    jQuery中的$.ajax()方法 $.ajax({ type:"POST", url:"../page/user.action?userId=" + use ...

  9. jquery中的ajax方法参数

    引用来自:http://www.cnblogs.com/tylerdonet/p/3520862.html jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String ...

  10. HTML5中的data-*属性和jQuery中的.data()方法使用

    原文地址链接:http://blog.csdn.net/fly_zxy/article/details/50687691: HTML5中的data-*属性 我们往往会根据需要在HTML标记上添加自定义 ...

随机推荐

  1. spring环境搭建

    1.导入jar包: 2.配置文件 — applicationContext.xml:  添加schema约束,位置:spring-framework-3.2.0.RELEASE—>docs—&g ...

  2. MATLAB 实时脚本(live-script)使用

    在matlab2016a及以上的版本不建议安装notebook来编写实施脚本,以为之后的matlab里面会有live-script,他可以创建实施脚本,使脚本与方便操作.那么这个live script ...

  3. android------个人项目(歆语气象通新版)

    歆语气象通: 歆语气象伴随你的身边,便捷生活. 包含了以下功能: 1. 天气预报数据覆盖中国城市和地区:2. 提供一周天气预报及最低最高温度,时刻关注天气,轻松计划出行:3. 各种指数详细信息,如太阳 ...

  4. 延迟提交form

    提交按钮延迟提交form表单 function a(){document.getElementById('form1').submit();}setTimeout(a,5000);

  5. Android CmakeList

    https://www.cnblogs.com/chenxibobo/p/7678389.html

  6. 非阻塞模式(ioctlsocket)

    //Server.cpp #include <stdio.h> #include <winsock2.h> //winsock.h (2种套接字版本) #pragma comm ...

  7. linux安装kibana

    下载kibana wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz -linux- ...

  8. [luogu P2633] Count on a tree

    [luogu P2633] Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点 ...

  9. nodejs和mongodb常见的小错误

    1.直接使用nodejs会遇到以下问题: module.js:340    throw err;         ^Error: Cannot find module 'mongodb' 解决方式如下 ...

  10. ldap 导出、导入ldif数据

    ldap 导出.导入ldif数据有如下方式: 1.dsadm(速度快,需要停止ldap实例) 2.dsconf(速度慢,需要保持ldap实例开启) windows导出.导入需要加上参数--unsecu ...