1、选择器+遍历

$('div').each(function (i){

i就是索引值

this 表示获取遍历每一个dom对象

});

2、选择器+遍历

$('div').each(function (index,domEle){

index就是索引值

domEle 表示获取遍历每一个dom对象

});

3、更适用的遍历方法

1)先获取某个集合对象

2)遍历集合对象的每一个元素

var d=$("div");

$.each(d,function (index,domEle){

d是要遍历的集合

index就是索引值

domEle 表示获取遍历每一个dom对

});

代码案例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin:0;padding:0;}
img{ display: block; }
div{ width: 100px; height: 30px;line-height: 30px; text-align: center; border:1px solid red; margin-bottom: 10px; }
.example{ border:1px solid red; width: 100px; height: 100px; }
.btn{ margin-top: 20px; overflow: hidden; display: block; }
</style>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。
// $('img').each(function(i){
// this.src="test"+i+'.jpg';
// })
//如果你想得到 jQuery对象,可以使用 $(this) 函数。
// $("img").each(function(){
// $(this).toggleClass("example");
// });
              //使用 'return' 来提前跳出 each() 循环。
$("button").click(function () {
$("div").each(function (index, domEle) {
// domEle == this
$(domEle).css("backgroundColor", "yellow");
if ($(this).is("#stop")) {
$("span").text("Stopped at div index #" + index);
return false;
}
});
});
});
</script> </head>
<body>
<p>each概述</p>
<p>以每一个匹配的元素作为上下文来执行一个函数</p>
<p>意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。而且,在每次执行函数时,
都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整型)。返回'false'将停止循环(就像在普通
的循环中使用'break')。返回'true'跳至下一个循环(就像在普通的循环中使用'continue')。</p>
<img />
<img /> <button class="btn">Change colors</button>
<span></span>
<div></div>
<div></div> <div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div> <div></div>
<div></div>
</body>
</html>

  

Jquery中each的更多相关文章

  1. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  2. JQuery中的工具函数总结

    前提引入 前提当然也是要引入Jquery啦... <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" typ ...

  3. JQuery中ajax的相关方法总结

    前提条件 话说是jquery中的ajax方法,那么前提条件当然是引入jquery啦. <script src="http://libs.baidu.com/jquery/1.9.0/j ...

  4. JavaScript jQuery 中定义数组与操作及jquery数组操作

    首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...

  5. jQuery中的100个技巧

      1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. < ...

  6. jquery中ajax用return来返回值无效

    jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...

  7. JQuery中$.each 和$(selector).each()的区别详解

    PS:晚上在写页面时,发现了一个问题,$.each 和$(selector).each()有哪些区别?百度搜索关键词,首页显示出来一些前人的经验,总结一下,发上来. 1.$(selector).eac ...

  8. jQuery中Animate进阶用法(一)

    jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...

  9. jquery 中jsonp的实现原理

    在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,即一般的 ajax是不能进行跨域请求的.但 img.iframe .script等标签是个例外,这些标签可以通过 src属性请求到其 ...

  10. 解决上一篇jquery中on的疑惑

    内容都是来自:http://www.365mini.com/page/jquery-on.htm.这里做一下收藏.文章的最后  疑问和解答可以解决所有的疑惑  看了之后能更好的整篇文章. on()函数 ...

随机推荐

  1. keepalived和zookeeper对比

    https://blog.csdn.net/vtopqx/article/details/79066703keepalived与zookeeper都可以用来实现高可用,高可用一般跟负载均衡会一起考虑, ...

  2. iOS 导航栏黑线,UIImage 枚举处理方式

      ios 找出导航栏下面的黑线(可隐藏,改变样式等) http://www.jianshu.com/p/effa4a48f1e3     设置UIImage的渲染模式:UIImage.renderi ...

  3. iOS开发时间控件怎么强制24小时制(小技巧)

    1)当你的format格式是 NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];[dateFo ...

  4. java.util.regex.PatternSyntaxException, dangling metacharacter “?” 解决方法

    今天在用正则表达式的时候遇到这样一个异常 看了相关资料后发现这是因为在正则表达式中,像"?","*","\"都是保留字符,所以在用的时候需要 ...

  5. ubuntu mysql导出数据库及数据

    mysqldump -u root -p mask_rcnn_realsense > /home/luo/mask_rcnn_realsense1.sql

  6. 714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票

    [抄题]: Your are given an array of integers prices, for which the i-th element is the price of a given ...

  7. CloudStack Ctrix官网版本

    手动生成keystore keytool -genkey -keystore /etc/cloudstack/management/cloud.keystore -storepass "vm ...

  8. linux操作系统下,怎么使用kill按照PID一次杀死多个进程

    1.ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9 说明:“grep firefox”的输出结果是,所有含有关 ...

  9. laravel @if

  10. g2o 图优化

    http://www.cnblogs.com/gaoxiang12/p/5244828.html 扩展里面csparse