jQuery.each()与.each()理解与用法

 



刚在学习写些jQuery的插件,当写得循环语句时自己有点蒙了,搞不清如何区分和使用jQuery.each()与.each()了。为了仔细了解他


们之间的异同,我很认真的查看了官方的文档(http://api.jquery.com/jQuery.each/)


 $.each()函数与.each()的不同在于:

.each()是通过一个jQuery对象来调用,遍历并执行对象内的每一个元素,示例:$(".box").each(function(){}),其遍历jQuery对象


集合里的每一个元素。

而$.each()刚可以用与遍历任何的集合,map,array之类的,当然对象也没问题。调用时只要将要遍历的对象作为第一个参数放进去


便行。示例:$.each([52, 97], function(index, value) { 

   alert(index + ': ' + value); 

});

像Array的话,回调函数内参数提供一个索引与其相应值。


官方文档原文:The $.each() function is not the same as .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 a map (JavaScript 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.


如有哪里写得不对,请大家指教~

 

个人有关JQuery插件的网站,请支持:jqueryhtml5.webege.com

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值