Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4249507
  • 博文数量: 601
  • 博客积分: 15410
  • 博客等级: 上将
  • 技术积分: 6884
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-16 08:11
个人简介

独学而无友,则孤陋而寡闻!

文章分类

全部博文(601)

文章存档

2020年(1)

2018年(4)

2017年(7)

2016年(42)

2015年(25)

2014年(15)

2013年(36)

2012年(46)

2011年(117)

2010年(148)

2009年(82)

2008年(37)

2007年(41)

分类: JavaScript

2015-01-07 10:38:14

今天又迷惘了一次,决定把这玩意记下来:

1、通用的each
 这种格式主要用于jquery的对象中,也可以用于数组。each前面没有jquery对象,只有jquery的标志:



  1. 格式:jQuery.each( array, callback )

  2.         array
  3.         Type: Array
  4.         The array to iterate over.
  5.         callback
  6.         Type: Function( Integer indexInArray, Object value )
  7.         The function that will be executed on every object.
  8.     jQuery.each( object, callback )
  9.         object
  10.         Type: Object
  11.         The object to iterate over.
  12.         callback
  13.         Type: Function( String propertyName, Object valueOfProperty )
  14.         The function that will be executed on every object.

  15. 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 thethiskeyword, but Javascript will always wrap thethisvalue as anObjecteven if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

用法示例:

  1. var obj = {
  2. "flammable": "inflammable",
  3. "duh": "no duh"
  4. };
  5. $.each( obj, function( key, value ) {
  6. alert( key + ": " + value );
  7. });

2、dom的eache,主要用于dom结构中,在对象和数组中有时不好用,需要认清使用的目标:

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

  2.     version added: 1.0.each( function )
  3.         function
  4.         Type: Function( Integer index, Element element )
  5.         A function to execute for each matched element.

  6. 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.
示例:

  1. // The .each() method is unnecessary here:
  2. $( "li" ).each(function() {
  3. $( this ).addClass( "foo" );
  4. });
  5. // Instead, you should rely on implicit iteration:
  6. $( "li" ).addClass( "bar" );

阅读(1753) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~