jquery .each
时间: 2023-08-31 08:05:14 浏览: 40
The jQuery .each() function is used to iterate over a set of elements and perform a function for each element in the set. It is similar to a for-loop, but with the added convenience of being able to access the current element with the "this" keyword.
Syntax:
```
$(selector).each(function(index, element){
// code to execute for each element
});
```
The parameters passed to the .each() function are:
- "selector": the set of elements to iterate over
- "function": the function to be executed on each element
The "index" parameter is the index of the current element in the set, while the "element" parameter is the current element itself.
Example:
```
$("li").each(function(index, element){
console.log("Element " + index + " contains " + $(this).text());
});
```
In this example, the .each() function is used to loop through all the "li" elements on the page and log their contents to the console. The "index" parameter is used to display the element's index in the set, while the "this" keyword is used to access the current element and retrieve its text content.