What does each mean in jquery

WBOY
Release: 2022-05-10 14:43:54
Original
2930 people have browsed it

In jquery, each means "looping through a set of elements"; the function of each() method is to traverse the specified objects and arrays, which is equivalent to the for loop in the program. Returning false can be used to stop early. Loop, the syntax is "$.each (object or array to be traversed, function used for loop execution)".

What does each mean in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What does each mean in jquery?

It means looping through a selected set of elements, which is equivalent to the for loop in the program

jQuery.each() function Used to traverse specified objects and arrays.

Syntax

$.each( object, callback )
Copy after login

object Object type specifies the object or array that needs to be traversed.

callback Function type specifies the function used for loop execution.

each() method specifies a function to run for each matching element.

Tip: Returning false can be used to stop the loop early.

The example is as follows:

Traverse the array:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<style>
div {
color: blue;
}
div#five {
color: red;
}
</style>
<script src="js/jquery.min.js"></script>
</head>
<body>
 
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
<div id="five"></div>
<script>
$(function () { 
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
$.each( arr, function( i, val ) {
$( "#" + val ).text( "我的是 " + val + "." );
// 在 "three" 之后将停止运行
return ( val !== "three" );
});
$.each( obj, function( i, val ) {
$( "#" + i ).append( document.createTextNode( " - " + val ) );
});
})
</script>
 
</body>
</html>
Copy after login

Output result:

What does each mean in jquery

Traverse Object:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<script>
$(function () { 
var obj = {
"flammable": "inflammable",
"duh": "no duh"
};
$.each( obj, function( key, value ) {
alert( key + ": " + value );
});
})
</script>
 
</body>
</html>
Copy after login

Output result:

What does each mean in jquery

What does each mean in jquery

## Recommended related video tutorial:

jQuery video tutorial

The above is the detailed content of What does each mean in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template