How to use the JQuery each () method _jquery

Source: Internet
Author: User
Both jquery and jquery objects implement this method, and for jquery objects, We simply delegate each method by simply delegating the jquery object as the first parameter to the each method of jquery. In other words: jquery provides each method to make a method call to all the child elements in the object provided by the parameter. The jquery object provides each method to invoke the child elements within jquery.
Copy Code 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 function's parameter args.
Let's discuss it according to the OJB object:

1.obj objects are arrays
Each method invokes the FN function of the neutron element of the array, until the result returned by 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.obj object is not an array
The biggest 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.
Copy Code code as follows:

Jquery.each=function (obj, FN, args) {
if (args) {
if (obj.length = = undefined) {
for (var i in obj)
Fn.apply (obj, args);
}else{
for (var i = 0, ol = Obj.length i < ol; i++) {
if (fn.apply (obj, args) = = False)
Break

}

}
} else {
if (obj.length = = undefined) {
for (var i in obj)
Fn.call (obj, I, obj);
}else{
for (var i = 0, ol = obj.length, val = obj[0]; I < ol && Fn.call (val,i,val)!== false; val = Obj[++i]) {}

}
}
return obj;
}

Of particular note is 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), which means that In your own implementation of FN, you can directly refer to an array or a child element of an object by using the this pointer. This approach is one of the most used implementations of jquery.
In jquery there is a each method, it is very cool to use, no longer like the original for The loop, the jquery source itself has a lot to use each method.
In fact, each of the methods in jquery is achieved through the call method in JS.
Here is a brief introduction to the call method.
Call is a fascinating method, but the official note is: "Invoke one of the methods of an object, replacing the current object with another object." "More explanations on the web are the changing context, and also the context this pointer."
Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Parameters
Thisobj
Options available. The object that will be used as the current object.
Arg1, Arg2, argn.
Options available. A sequence of method parameters is passed.
Description
The call method can be used to invoke a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.
There's a classic example of a quote on the web.
JS Code
Copy Code code as follows:

function Add (a,b)
{
alert (A+B);
}
function sub (a,b)
{
alert (a-b);
}
Add.call (sub,3,1);

Use Add to replace Sub,add.call (sub,3,1) = = Add (3,1), so the result is: alert (4);
Note: The function in JS is actually an object, the function name is a reference to a function object.
Specific call more in-depth is not mentioned here.
Here are some common uses of each method of jquery
JS Code
Copy Code code as follows:

var arr = ["One", "two", "three", "four"];
$.each (arr, function () {
alert (this);
}); The results of the
//above each output are: One,two,three,four
var arr1 = [[1, 4, 3], [4, 6, 6], [7, 9]]
$.each (arr1, function ( I, item) {
Alert (item[0]);
});
//In fact arr1 is a two-dimensional array, the item is equivalent to taking each one-dimensional array,
//item[0] relative to the first value in each one-dimensional array
//So the above each output is: 1 4 7
var obj = {one: 1, Two:2, Three:3, four:4};
$.each (obj, function (key, Val) {
alert (Obj[key]);
});
//This each one is more powerful, can cycle each of the properties
//Output is: 1 2 3 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.