The each () method simplifies the DOM loop structure and is not prone to errors. The each () function encapsulates powerful traversal functions and is easy to use. It can traverse one-dimensional arrays, multi-dimensional arrays, DOM, JSON, and so on, using $ each in javaScript development can greatly reduce our workload.
Var arr = ["one", "two", "three", "four"]; $. each (arr, function () {alert (this );});
The above each output results are: one, two, three, four
Var arr = ["aaa", "bbb", "ccc"]; $. each (arr, function (I, a) {alert (I); // I is the ordinal number of the loop alert (a); // a is the value }); 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, and item is equivalent to taking every one-dimensional array,
Item [0] is relative to the first value in each one-dimensional array.
Therefore, 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]); alert (key); // key alert (val); // value });
Output result: 1 2 3 4
The above is all the content of this article. I hope you will like it.