I wrote a small plug-in yesterday and used this method. I don't quite understand the $. each () method. I checked the manual today and learned how to use arrays and element indexes and content. (I is the index, n is the content)
The Code is as follows:
$. Each ([0, 1, 2], function (I, n ){
Alert ("Item #" + I + ":" + n );
});
Sample object and use both the member name and variable content. (I is the member name, and n is the variable content)
The Code is as follows:
$. Each ({name: "John", lang: "JS"}, function (I, n ){
Alert ("Name:" + I + ", Value:" + n );
});
This example uses an input form element as an example.
If you have such code in the dom
Then you use the each as follows:
The Code is as follows:
$. Each ($ ("input: hidden"), function (I, val ){
Alert (val); // output [object HTMLInputElement] because it is a form element.
Alert (I); // The output index is 0, 1, 2, 3.
Alert (val. name); // output the value of name
Alert (val. value); // output value
});
The following is an official explanation:
JQuery. each (object, [callback])
Overview
The general example method can be used for example object and array.
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 object's member or array index, and the second is the corresponding variable or content. If you want to exit the each loop, the callback function returns false. Other return values are ignored.
Parameters
ObjectObject
Objects or arrays that need to be repeated.
Callback (optional) Function
The callback function executed by each member/element.