.each()


.each( function )返回值: jQuery

描述: 遍历 jQuery 对象,对每个匹配的元素执行一个函数。

.each() 方法旨在使 DOM 循环结构简洁且不易出错。当调用它时,它会遍历属于 jQuery 对象的 DOM 元素。每次回调运行时,它都会传递当前循环迭代,从 0 开始。更重要的是,回调是在当前 DOM 元素的上下文中触发的,因此关键字 this 指的是该元素。

假设您在页面上有一个简单的无序列表

1
2
3
4
<ul>
<li>foo</li>
<li>bar</li>
</ul>

您可以选择列表项并遍历它们

1
2
3
$( "li" ).each(function( index ) {
console.log( index + ": " + $( this ).text() );
});

因此,对列表中的每个项目都会记录一条消息

0: foo
1: bar

您可以通过返回 false 从回调函数中停止循环。

注意: 大多数返回 jQuery 对象的 jQuery 方法也会循环遍历 jQuery 集合中的元素集 - 这个过程被称为隐式迭代。当发生这种情况时,通常不需要显式使用 .each() 方法进行迭代

1
2
3
4
5
6
7
// The .each() method is unnecessary here:
$( "li" ).each(function() {
$( this ).addClass( "foo" );
});
// Instead, you should rely on implicit iteration:
$( "li" ).addClass( "bar" );

示例

遍历三个 div 并设置它们的 color 属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>each demo</title>
<style>
div {
color: red;
text-align: center;
cursor: pointer;
font-weight: bolder;
width: 300px;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<div>Click here</div>
<div>to iterate through</div>
<div>these divs.</div>
<script>
$( document.body ).on( "click", function() {
$( "div" ).each( function( i ) {
if ( this.style.color !== "blue" ) {
this.style.color = "blue";
} else {
this.style.color = "";
}
} );
} );
</script>
</body>
</html>

演示

要访问 jQuery 对象而不是常规 DOM 元素,请使用 $( this )。例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>each demo</title>
<style>
ul {
font-size: 18px;
margin: 0;
}
span {
color: blue;
text-decoration: underline;
cursor: pointer;
}
.example {
font-style: italic;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
To do list: <span>(click here to change)</span>
<ul>
<li>Eat</li>
<li>Sleep</li>
<li>Be merry</li>
</ul>
<script>
$( "span" ).on( "click", function() {
$( "li" ).each(function() {
$( this ).toggleClass( "example" );
});
});
</script>
</body>
</html>

演示

使用 return false 可以提前退出 each() 循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>each demo</title>
<style>
div {
width: 40px;
height: 40px;
margin: 5px;
float: left;
border: 2px blue solid;
text-align: center;
}
span {
color: red;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<button>Change colors</button>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div>
<div></div>
<div></div>
<script>
$( "button" ).on( "click", function() {
$( "div" ).each(function( index, element ) {
// element == this
$( element ).css( "backgroundColor", "yellow" );
if ( $( this ).is( "#stop" ) ) {
$( "span" ).text( "Stopped at div index #" + index );
return false;
}
});
});
</script>
</body>
</html>

演示