Home >Web Front-end >JS Tutorial >What is the usage form of each in jquery

What is the usage form of each in jquery

WBOY
WBOYOriginal
2021-12-23 17:41:321773browse

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. .

What is the usage form of each in jquery

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:

What is the usage form of each in jquery

After clicking the button, the output is as follows:

What is the usage form of each in jquery

What is the usage form of each in jquery

What is the usage form of each in jquery

# #Related video tutorial recommendations:

jQuery video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn