An array of examples, using both element indexes and content. (i is index, n is content)
Copy Code code as follows:
$.each ([0,1,2], function (i, n) {
Alert ("Item #" + i + ":" + N);
});
The instance object, with the member name and variable content. (I is the member name, n is the variable content)
Copy Code code as follows:
$.each ({name: "John", Lang: "JS"}, function (i, n) {
Alert ("Name:" + i + ", Value:" + N);
});
Example of a DOM element, where an input form element is used as an example.
If you have a section of this code in the DOM
<input name= "AAA" type= "hidden" value= "a"/>
<input name= "BBB" type= "hidden" value= "222"/>
<input name= "CCC" type= "hidden" value= "333"/>
<input name= "ddd" type= "hidden" value= "444"/>
Then you use each of the following
Copy Code code as follows:
$.each ($ ("Input:hidden"), function (I,val) {
Alert (val); Output [Object Htmlinputelement] because it is a form element.
alert (i); Output Index is 0,1,2,3
alert (val.name); The value of the output name
alert (Val.value); Value of output value
});
The following is an official explanation:
Jquery.each (object, [callback])
Overview
A universal example method, which can be used for example objects and arrays.
Unlike the $ (). each () method of the JQuery object, this method can be used to sample any object. The callback function has two parameters: the first is the index of the member or array of the object, and the second is the corresponding variable or content. If you need to exit each loop so that the callback function returns false, the other return values are ignored.
Parameters
Objectobject
An object or array that needs to be repeated.
Callback (optional) Function
The callback function that each member/element executes.