The
each () function is a tool-class function that basically all of the frameworks provide, through which you can traverse objects, array property values, and process them, both jquery and jquery objects implement the method, and here's how to use
For jquery objects, we simply delegate each method by simply delegating the jquery object as the first argument to the each method of jquery. In other words: jquery provides each method to make a method call to all of the child elements in the object provided by the parameter. The jquery object provides each method to invoke the child elements within jquery. Code as follows: Jquery.prototype.each=function (FN, args) { return Jquery.each (this, FN, args); } Let's look at the concrete implementation of each method provided by JQuery, Jquery.each (Obj,fn,arg) The method has three parameters: the object obj to operate, the function FN to operate, and the parameter of the function args. Let's discuss it according to the OJB object: 1. An obj object is an array each method that invokes the FN function of a neutron element on a group of elements until the result of a call to a child element is false, that is, we can exit each method call when the supplied FN function is processed so that it satisfies a certain condition. When the arg argument is supplied by the each method, the argument passed in by the FN function is arg, otherwise: the child element Index, the child element itself. 2. The Obj object is not an array The maximum difference between this method and 1 is that the FN method is carried out without regard to the return value. In other words, all the properties of the Obj object are invoked by the FN method, even if the FN returns false. Calling incoming arguments is similar to 1. Code as follows: Jquery.each=function (obj, FN, args) { if (args) { &N Bsp;if (obj.length = = undefined) { for (var i in obj) &NBSP ; fn.apply (obj, args); }else{ for (var i = 0, ol = obj.length; i < ol; i+ +) { if (fn.apply (obj, args) = = false) & nbsp break; { &NBSP} & nbsp;} else { if (obj.length = = undefined) { for (v Ar i in obj) fn.call (obj, I, obj); &NB Sp }else{ for (var i = 0, ol = obj.length, val = obj[0]; i < ol && Fn.call (Val,i,val)!== false; val = obj[++i]) {} &NBSP} } return obj; } requires special note It means that the specific invocation method of FN in each method does not take the form of a simple FN (i,val) or FN (args), but rather a fn.call (val,i,val) or fn.apply (Obj.args), meaning that theIn your own implementation of the FN, you can use the this pointer directly to refer to the array or the child elements of the object. This approach is one of the most used implementations of jquery.