-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathmap.xml
160 lines (155 loc) · 4.86 KB
/
map.xml
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?xml version="1.0"?>
<entry type="method" name="map" return="jQuery">
<title>.map()</title>
<signature>
<added>1.2</added>
<argument name="callback" type="Function">
<argument name="index" type="Integer" />
<argument name="domElement" type="Element" />
<return type="Object" />
<desc>A function object that will be invoked for each element in the current set.</desc>
</argument>
</signature>
<desc>Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.</desc>
<longdesc>
<p>If you wish to process a plain array or object, use the <a href="/jQuery.map/">jQuery.map()</a> instead. </p>
<p>As the return value is a jQuery object, which contains an array, it's very common to call <code>.get()</code> on the result to work with a basic array.</p>
<p>The <code>.map()</code> method is particularly useful for getting or setting the value of a collection of elements. Consider a form with a set of checkboxes in it:</p>
<pre><code>
<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>
</code></pre>
<p>To get a comma-separated list of checkbox <code>ID</code>s:</p>
<pre><code>
$( ":checkbox" )
.map(function() {
return this.id;
})
.get()
.join();
</code></pre>
<p>The result of this call is the string, <code>"two,four,six,eight"</code>.</p>
<p>Within the callback function, <code>this</code> refers to the current DOM element for each iteration. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns <code>null</code> or <code>undefined</code>, no element will be inserted.</p>
</longdesc>
<example>
<desc>Build a list of all the values within a form.</desc>
<code><![CDATA[
$( "p" )
.append( $( "input" ).map(function() {
return $( this ).val();
})
.get()
.join( ", " ) );
]]></code>
<css><![CDATA[
p {
color: red;
}
]]></css>
<html><![CDATA[
<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>
]]></html>
</example>
<example>
<desc>A contrived example to show some functionality.</desc>
<code><![CDATA[
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 );
]]></code>
<css><![CDATA[
body {
font-size: 16px;
}
ul {
float: left;
margin: 0 30px;
color: blue;
}
#results {
color: red;
}
]]></css>
<html><![CDATA[
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
</ul>
<ul id="results">
</ul>
]]></html>
</example>
<example>
<desc>Equalize the heights of the divs.</desc>
<code><![CDATA[
$.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();
} );
]]></code>
<css><![CDATA[
div {
width: 40px;
float: left;
}
input {
clear: left;
}
]]></css>
<html><![CDATA[
<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>
]]></html>
</example>
<category slug="traversing/filtering"/>
<category slug="version/1.2"/>
</entry>