Home  >  Article  >  Web Front-end  >  Examples of several common usage methods of jquery each_jquery

Examples of several common usage methods of jquery each_jquery

WBOY
WBOYOriginal
2016-05-16 17:03:121040browse

There are also many uses of each method in the jQuery source code. In fact, the each method in jQuery is implemented through the call method in js. The following is a brief introduction to the call method.

The call method is very wonderful. In fact, the official description is: "Call a method of an object and replace the current object with another object." More explanations on the Internet are to change the context environment, and some say to change the context this pointer.

Copy code The code is as follows:

call([thisObj[,arg1[, arg2[, [,.argN]]]]])


Parameters

thisObj Optional. The object that will be used as the current object.
arg1, arg2, , argN optional. A sequence of method parameters will be passed.

The call method can be used to call a method on behalf of another object. The call method changes the object context of a function from the initial context to the new object specified by thisObj.

Example

Copy code The code is as follows:

function add(a,b)
{
alert(a b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub, 3,1);


Replace sub with add, add.call(sub,3,1) == add(3,1), so the running result is: alert(4 );
Note: Functions in js are actually objects, and the function name is a reference to the Function object.
I won’t mention the specific call in more detail here.

Here are some common uses of jQuery’s each method

Copy code The code is as follows:

var arr = [ "one", "two", "three", "four"];
$.each(arr, function(){
alert(this);
});

The output of each above The results are: one, two, three, four

Copy code The code is as follows:

var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]] 
$.each(arr1, function(i, item){ 
alert(item[0]); 
});

In fact, arr1 is a two-dimensional array, item is equivalent to taking each one-dimensional array,
item[0] is equivalent to taking the first value in each one-dimensional array
So the above each output They are: 1 4 7 { one:1, two:2, three:3, four:4}; 
$.each(obj, function(key, val) { 
alert(obj[key]); 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn