For details about how to use the each method in jQuery, jqueryeach
Overview:
The each () method specifies the function to be run for each matching element.
Returns false to stop the loop as early as possible, which is equivalent to break.
Returns true to end the loop, which is equivalent to continue.
Syntax:
$ (Selector ). each (function (index, element) {}) index-selector index position element-Current element (you can also use the "this" selector) $ (selector ). each (function () {}) $. each (array, function (Key, Value ){})
1. Traverse js Array
$ (Function () {var array = ["aaa", "bbb", "ccc"]; $. each (array, function (I, j) {alert (I + ":" + j); // I indicates the index, j indicates the value });})
2. Traverse Object
Var obj = new Object (); obj. name = "zs"; $. each (obj, function (name, value) {alert (this); // this points to the value of the current attribute, which is equivalent to value alert (name ); // name indicates the name of the current Object attribute alert (value); // value indicates the value of the current Object attribute });
3. Traverse JSON objects
var json ={"name":"zhangSan","role":"student"};$.each(json,function(key,value){ alert(key+":"+value);});
4. Traverse arrays composed of multiple JSON objects
var json =[{"name":"Amy","role":"student"},{"name":"Tom","role":"student"}];$.each(json, function(index, value) { alert("index="+index+"\n" +"name:"+value.name+"\n"+"role:"+value.role+"\n");});
5. Traverse jQuery objects
<Head> <meta charset = "UTF-8"/> <title> traverse jQuery objects </title> <script src = "js/jquery-1.12.4.js"> </script> <script type = "text/javascript" >$ (function () {$ ("input [type = 'click']"). bind ("click", function () {$ ("li "). each (function () {alert ($ (this ). text ())});});}); </script>
Summary
The above is a detailed description of how to use the each method in jQuery. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!