The each () method makes the DOM loop structure simple and error-prone. The each () function encapsulates a very powerful traversal function and is also handy for traversing one-dimensional arrays, multidimensional arrays, DOM, JSON, and so on, and using $each during JavaScript development can greatly reduce our workload.
1 2 3 4 |
var arr = ["One", "two", "three", "four"]; $.each (arr, function () {alert (this); }); |
The results of each output above are: One,two,three,four
1 2 3 4 5 6 7 8 9 |
var arr = ["AAA", "BBB", "CCC"]; $.each (arr, function (i,a) {alert (i);//I is circular ordinal alert (a);//A Is value}); 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 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
1 2 3 4 5 6 |
var obj = {one:1, two:2, Three:3, four:4}; $.each (obj, function (key, Val) {alert (Obj[key]); alert (key);//Key alert (val);//value}); |
The output is: 1 2 3 4