$ (). Each, for this method, is used more in DOM processing. If the page has more than one input label type checkbox, for this time use $ (). Each to handle multiple checkbook, for example:
The code is as follows |
Copy Code |
$ ("input[name= ' ch ']"). each (function (i) { if ($ (this). attr (' checked ') ==true) { Some action code } |
The callback function is a parameter that can be passed, and I is the index of the traversal.
For traversing an array, using $.each () to deal with, it is very cool to the extreme. For example:
The code is as follows |
Copy Code |
$.each ([{"Name": "Limeng", "email": "Xfjylimeng"},{"name": "hehe", "email": "Xfjylimeng"},function (i,n) { Alert ("Index:" +i, "corresponding value is:" +n.name); }); |
Parameter i is traversing the index value and n is the current traversal object.
code is as follows |
copy code |
var arr1 = [" One "," two "," three "," four "," five "]; $.each (arr1, function () { alert (this); }); Output:one two three four five var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] $ . each (arr2, function (I, item) { Alert (item[0]); }; Output:1 4 7 var obj = {one:1, two:2, Three:3, Four:4, five:5}; $.each (obj, function (ke Y, val) { alert (Obj[key]); }; Output:1 2 3 4 5 |
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
The code is as follows |
Copy Code |
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
code is as follows |
copy code |
JS code 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); }); //Actually arr1 is a two-dimensional array, the item is equivalent to taking every 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 loop each attribute //output to:1 2 3 4 |