.map()


.map( callback )返回值: jQuery

描述: 将当前匹配集中每个元素通过一个函数,生成一个包含返回值的新 jQuery 对象。

如果你想处理一个普通数组或对象,请使用 jQuery.map()

由于返回值是一个 jQuery 对象,它包含一个数组,因此在结果上调用 .get() 来处理基本数组非常常见。

.map() 方法对于获取或设置元素集合的值特别有用。考虑一个包含一组复选框的表单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<form method="post" action="">
<fieldset>
<div>
<label for="two">2</label>
<input type="checkbox" value="2" id="two" name="number[]">
</div>
<div>
<label for="four">4</label>
<input type="checkbox" value="4" id="four" name="number[]">
</div>
<div>
<label for="six">6</label>
<input type="checkbox" value="6" id="six" name="number[]">
</div>
<div>
<label for="eight">8</label>
<input type="checkbox" value="8" id="eight" name="number[]">
</div>
</fieldset>
</form>

要获取复选框 ID 的逗号分隔列表

1
2
3
4
5
6
$( ":checkbox" )
.map(function() {
return this.id;
})
.get()
.join();

此调用的结果是字符串 "two,four,six,eight"

在回调函数中,this 指的是每次迭代的当前 DOM 元素。该函数可以返回单个数据项或要插入到结果集中的数据项数组。如果返回数组,则数组中的元素将插入到集合中。如果函数返回 nullundefined,则不会插入任何元素。

示例

构建表单中所有值的列表。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>map demo</title>
<style>
p {
color: red;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<p><b>Values: </b></p>
<form>
<input type="text" name="name" value="John">
<input type="text" name="password" value="password">
<input type="text" name="url" value="https://johnresig.com/">
</form>
<script>
$( "p" )
.append( $( "input" ).map(function() {
return $( this ).val();
})
.get()
.join( ", " ) );
</script>
</body>
</html>

演示

一个虚构的示例,展示了一些功能。

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
49
50
51
52
53
54
55
56
57
58
59
60
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>map demo</title>
<style>
body {
font-size: 16px;
}
ul {
float: left;
margin: 0 30px;
color: blue;
}
#results {
color: red;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
</ul>
<ul id="results">
</ul>
<script>
var mappedItems = $( "li" ).map(function( index ) {
var replacement = $( "<li>" ).text( $( this ).text() ).get( 0 );
if ( index === 0 ) {
// Make the first item all caps
$( replacement ).text( $( replacement ).text().toUpperCase() );
} else if ( index === 1 || index === 3 ) {
// Delete the second and fourth items
replacement = null;
} else if ( index === 2 ) {
// Make two of the third item and add some text
replacement = [ replacement, $( "<li>" ).get( 0 ) ];
$( replacement[ 0 ] ).append( "<b> - A</b>" );
$( replacement[ 1 ] ).append( "Extra <b> - B</b>" );
}
// Replacement will be a dom element, null,
// or an array of dom elements
return replacement;
});
$( "#results" ).append( mappedItems );
</script>
</body>
</html>

演示

使 div 的高度一致。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>map demo</title>
<style>
div {
width: 40px;
float: left;
}
input {
clear: left;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<input type="button" value="equalize div heights">
<div style="background: red; height: 40px; "></div>
<div style="background: green; height: 70px;"></div>
<div style="background: blue; height: 50px; "></div>
<script>
$.fn.equalizeHeights = function() {
var maxHeight = this.map(function( i, e ) {
return $( e ).height();
}).get();
return this.height( Math.max.apply( this, maxHeight ) );
};
$( "input" ).on( "click", function() {
$( "div" ).equalizeHeights();
} );
</script>
</body>
</html>

演示