Home >Web Front-end >JS Tutorial >What is the usage form of each in jquery
The usage form of each in jquery is "$(selector).each(function(index,element))". The each() method can specify the function to be run by matching elements. It is often used to traverse specified objects and arrays. .
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
What is the usage form of each in jquery
each() method specifies the function to be run for each matching element.
Tip: Returning false can be used to stop the loop early.
The syntax is as follows:
$(selector).each(function(index,element))
The parameters are expressed as follows:
function(index,element) is a required parameter, indicating the function to be run for each matching element.
index represents the index position of the selector, element represents the current element (the "this" selector can also be used)
The example is as follows
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("li").each(function(){ alert($(this).text()) }); }); }); </script> </head> <body> <button>输出每个列表项的值</button> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html>
Output result:
After clicking the button, the output is as follows:
# #Related video tutorial recommendations:The above is the detailed content of What is the usage form of each in jquery. For more information, please follow other related articles on the PHP Chinese website!