您的位置:首页 > Web前端 > JQuery

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

2010-12-07 11:47 435 查看
刚在学习写些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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: