1, $.each ()--Global function introduction
Syntax: global function Jquery.each (collection,function (indexinarray,valueofelement))
collection--can be any collection, whether it is a JS object or an array, or a JSON object.
The key of the indexinarray--object or the index of the array
valueofelement--the value of an object or an item of an array
Traversing a one-dimensional array
<script> var arr = [' A ', ' B ']; Jquery.each (arr,function(index,value) { console.log (index); Console.log (value); }) </script>
Results:
Traversing a two-dimensional array
<script> var arr = [[' A ', ' B '],[' C ', ' d ']; Jquery.each (arr,function(index,value) { Jquery.each (value,function(i, Element) { console.log (i); Console.log (Element);}) }) </script>
Results:
Traversing objects
<script> var obj = { ' Zs ', + }; Jquery.each (obj,function(index,value) { console.log (index); Console.log (value); }) </script>
Results:
Traversing JSON objects
var obj = {"A": "Hello", "B": "World"}; Jquery.each (obj,function(index,value) { Console.log (index); Console.log (value);})
Results:
1, $ (selector). each ()
Syntax: $ (selector). Each (function(index,value))
index--the index of a pseudo-array
value--the item value of a pseudo-array
Dealing with more of the above on the DOM
Traverse the LI Tag
<ul> <li>1</li> <li>2</li> <li>3</li> <li>4 </li> <li>5</li> </ul> <script> $ (' Li '). each ( function(index,value) { console.log (index); /* print a string of shape <li>1</li> */ Console.log (value); /* If you want to output data from a tag, you can look at the bar as a DOM object . Then use the innerHTML method to output the data in the tag, or you can convert it to a jquery object shaped like $ (value). HTML (), and then the printout, as follows:*/ Console.log (value.innerhtml); }) </script>
Summary
Traverse data: Jquery.each (Collection,function (indexinarray,valueofelement));
Traverse dom:$ (selector). each (function (index,value));
each () function of jquery