Home >Web Front-end >Front-end Q&A >What does each mean in jquery
In jquery, each means "looping through a set of elements"; the function of each() method is to traverse the specified objects and arrays, which is equivalent to the for loop in the program. Returning false can be used to stop early. Loop, the syntax is "$.each (object or array to be traversed, function used for loop execution)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
It means looping through a selected set of elements, which is equivalent to the for loop in the program
jQuery.each() function Used to traverse specified objects and arrays.
Syntax
$.each( object, callback )
object Object type specifies the object or array that needs to be traversed.
callback Function type specifies the function used for loop execution.
each() method specifies a function to run for each matching element.
Tip: Returning false can be used to stop the loop early.
The example is as follows:
Traverse the array:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <style> div { color: blue; } div#five { color: red; } </style> <script src="js/jquery.min.js"></script> </head> <body> <div id="one"></div> <div id="two"></div> <div id="three"></div> <div id="four"></div> <div id="five"></div> <script> $(function () { var arr = [ "one", "two", "three", "four", "five" ]; var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 }; $.each( arr, function( i, val ) { $( "#" + val ).text( "我的是 " + val + "." ); // 在 "three" 之后将停止运行 return ( val !== "three" ); }); $.each( obj, function( i, val ) { $( "#" + i ).append( document.createTextNode( " - " + val ) ); }); }) </script> </body> </html>
Output result:
Traverse Object:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"></script> </head> <body> <script> $(function () { var obj = { "flammable": "inflammable", "duh": "no duh" }; $.each( obj, function( key, value ) { alert( key + ": " + value ); }); }) </script> </body> </html>
Output result:
## Recommended related video tutorial:The above is the detailed content of What does each mean in jquery. For more information, please follow other related articles on the PHP Chinese website!