The main function of each() method in jQuery is to loop through different data. We can use it to loop through multiple DOM objects from the same selector
What I will introduce to you today is the usage of each() function in jQuery, which allows us to loop through different data, such as arrays or objects. The each() function in jQuery is one of the most commonly used functions in jQuery. The following article will introduce the use of this method in detail.
【Recommended courses: jQuery Tutorial】
##The each() function in jQuery is used to loop data, similar to the for each loop. So we can use this to loop through multiple DOM objects from the same selectoreach() method
Specifies the function to be run for each matching element.$(selector).each(function(index,element))
$("a").each(function(){ $(this); })
Get the current index of the loop
<body> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <script src="jquery/jquery-1.12.4.js"></script> <script> $(function(){ $("li").each(function(i){ console.log(i); }) }) </script> </body>
Loop through an array:
<script src="jquery/jquery-1.12.4.js"></script> <script> var array=['Chinese','Math','English'] $.each(array,function(index,value){ console.log(index+":"+value); }) </script>
#Loop object:
You can use it to traverse the object and get the index value and position in the object.<script src="jquery/jquery-1.12.4.js"></script> <script> var obj={ name:"张三", age:"18", subject:"English" }; $.each(obj,function(index,value){ console.log("信息:"+index+":"+value); }) </script>
The above is the detailed content of What does the each() method in jQuery do?. For more information, please follow other related articles on the PHP Chinese website!