.map()
返回: jQuery
.map( callback )
描述:把当前匹配集合中的每个元素传递给一个函数,制造出一个包含返回的值的新jQuery对象。
加入于: 1.2
.map( callback )如果你想处理一个纯数组或纯对象,请使用jQuery.map()代替它。
因为返回的值是一个jQuery对象,它包含了一个数组,通常会对结果调用.get(),以处理基本数组。
要想读取或者设置一个元素集合的值,.map()方法很有用。设想一个表单,带有一个勾选框集合:
<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的一个逗号分隔的列表,只要:
$( ":checkbox" )
.map(function() {
return this.id;
})
.get()
.join();
这个调用的结果是一个字符串,"two,four,six,eight"。
在回调函数内部,this引用针对每次迭代的当前DOM元素。这个函数可以返回一个独立的数据项,或者数据项的一个数组,以插入到结果集合中。如果返回了数组,数组中的元素会插入到集合中。如果函数返回了null或undefined,就不会插入任何元素。
示例
在表单内部建立一个所有值的列表:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>map demo</title>
<style>
p {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.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="http://ejohn.org/">
</form>
<script>
$( "p" )
.append( $( "input" ).map(function() {
return $( this ).val();
})
.get()
.join( ", " ) );
</script>
</body>
</html>
演示结果
一个人为的示例,以显示一些功能。
<!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.jquery.com/jquery-1.10.2.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 ) {
// 使第一项全都大写。
$( replacement ).text( $( replacement ).text().toUpperCase() );
} else if ( index === 1 || index === 3 ) {
// 删除第二项和第四项。
replacement = null;
} else if ( index === 2 ) {
// 制作两个第三项,并添加一些文本
replacement = [ replacement, $( "<li>" ).get( 0 ) ];
$( replacement[ 0 ] ).append( "<b> - A</b>" );
$( replacement[ 1 ] ).append( "Extra <b> - B</b>" );
}
// 替代物将是一个dom元素、null、
// 或者是一个dom元素的数组
return replacement;
});
$( "#results" ).append( mappedItems );
</script>
</body>
</html>
演示结果
补偿div的高度。
<!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.jquery.com/jquery-1.10.2.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" ).click(function() {
$( "div" ).equalizeHeights();
});
</script>
</body>
</html>
演示结果