在jQuery中 $().each与$.each是不同的,$().each用于对jQuery对象做遍历操作处理,而$.each用于循环遍历一个Array或Object对象,相当于for或while循环写法。

.each( function )

Description: Iterate over a jQuery object, executing a function for each matched element.

.each( function )

  • function
    Type: FunctionInteger index, Element element )
    A function to execute for each matched element.

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

<ul>
<li>foo</li>
<li>bar</li>
</ul>
$( "li" ).each(function( index ) {
console.log( index + ": " + $( this ).text() );
});

jQuery.each( array, callback )

Description: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

$.each([ 52, 97 ], function( index, value ) {
alert( index + ": " + value );
}); var obj = {
"flammable": "inflammable",
"duh": "no duh"
};
$.each( obj, function( key, value ) {
alert( key + ": " + value );
});

jQuery中$().each与$.each的区别的更多相关文章

  1. JQuery对象与DOM对象的区别与转换

      1.jQuery对象和DOM对象的区别 DOM对象,即是我们用传统的方法(javascript)获得的对象,jQuery对象即是用jQuery类库的选择器获得的对象; eg: var domObj ...

  2. 【jQuery】【转】jQuery中的trigger和triggerHandler区别

    trigger(event, [data]) 在每一个匹配的元素上触发某类事件. 这个函数也会导致浏览器同名的默认行为的执行.比如,如果用trigger()触发一个'submit',则同样会导致浏览器 ...

  3. jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )

    jquery中attr和prop的区别. 什么时候用 attr 什么时候用 prop   在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这 ...

  4. JQuery中的html(),text(),val()区别

    jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...

  5. jQuery中.attr()和.prop()的区别

    之前学习jQuery的时候,学习到了两种取得标签的属性值的方法:一种是elemJobj.attr(),另一种是elemJobj.prop().而在学习JS的时候,只有一种方法elemObj.getAt ...

  6. jquery中attr和prop的区别(转)

    在网络上看到这样一篇关于jquery中attr和prop的区别文章,觉得不错,所以转载了. 在jQuery 1.6中,.attr()方法查询那些没有设置的属性,则会返回一个undefined.如果你要 ...

  7. Jquery中的 height(), innerHeight() outerHeight()区别

    jQuery中的 height innerHeight outerHeight区别 标准浏览器下: height:高度 innerHeight:高度+补白 outerHeight:高度+补白+边框,参 ...

  8. JQuery之append和appendTo的区别,还有js中的appendChild用法

    JQuery之append和appendTo的区别 append()前面是要选择的对象,后面是要在对象内插入的元素内容 appendTo()前面是要插入的元素内容且为Jquery对象,而后面是要选择的 ...

  9. jQuery学习-----(二)JQuery对象与DOM对象的区别与转换

    1.jQuery对象和DOM对象的区别 DOM对象,即是我们用传统的方法(javascript)获得的对象,jQuery对象即是用jQuery类库的选择器获得的对象; eg: var domObj = ...

  10. 基于jquery中children()与find()的区别介绍

    本篇文章介绍了,基于jquery中children()与find()的区别,需要的朋友参考下 .children(selector) 方法是返回匹配元素集合中每个元素的所有子元素(仅儿子辈).参数可选 ...

随机推荐

  1. bzoj1264 [AHOI2006]基因匹配Match 树状数组+lcs

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1255  Solved: 835[Submit][ ...

  2. Hibernate的注释该如何使用?每一个注释代表什么意思?

    出自:java快快飞 原文地址:http://blog.sina.com.cn/s/blog_697b968901016s31.html Hibernate的注释该如何使用?每一个注释代表什么意思? ...

  3. rest项目的基础返回类设计

    package com.hmy.erp.api.vo; import java.io.Serializable; import lombok.Data; /** * erp基本状态返回类 * * @a ...

  4. InnoDB的多版本并发控制(MMVC)

    InnoDB的MVCC之(乐观锁),是通过在每行记录保存两个隐藏列来实现的.这两个列,一个是存创建时间,一个是删除时间,这里的时间指的是,系统版本号,并不是真正的时间值. 每开始一个新的事务,系统版本 ...

  5. 更改mvc版本的时候,手动修改交3.0改到4.0,将razor改到2.0,仍然提示出现错误,mvc3.0

    偶然碰到,更改mvc版本,由3.0升级到4.0,但是依然提示3.0的错误 几经周折排查,最终发现 <runtime> <assemblyBinding xmlns="urn ...

  6. NEMA协议详解

    NEMA协议的由来 NMEA协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The   National Marin ...

  7. MFC 获得各类指针、句柄的方法(转)

    原文转自 https://blog.csdn.net/abcjennifer/article/details/7480019 1.MFC中获取常见类句柄<视图类,文档类,框架类,应用程序类> ...

  8. linux反汇编

    使用objdump参数可以: -a, --archive-headers    显示压缩头信息   -f, --file-headers       显示目录头总览   -p, --private-h ...

  9. javaweb核心技术servlet

      一.Servlet简介 1.什么是Servlet Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质 ...

  10. SQL 并发-转

    脏读.不可重复读  共享锁.悲观锁 和 事务五种隔离级别   一.脏读.不可重复读.幻读1.脏读:脏读就是指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个 ...