jquery each
The jQuery ".each()" method is used to iterate over a collection of elements and perform a function on each element individually.
The syntax for using the ".each()" method is as follows:
$(selector).each(function(index, element) {
// code to be executed on each element
});
- The
selectoris used to select the elements you want to iterate over. - The
indexparameter represents the index of the current element in the collection (starting from 0). - The
elementparameter represents the current element in the iteration.
Here's an example of using the ".each()" method to iterate over a list of elements and change their background color:
HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
JavaScript (jQuery):
$('li').each(function(index, element) {
$(element).css('background-color', 'red');
});
In this example, the ".each()" method is used to iterate over each "li" element and change its background color to red
原文地址: http://www.cveoy.top/t/topic/iJl2 著作权归作者所有。请勿转载和采集!