-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathclosest.xml
More file actions
167 lines (157 loc) · 6.85 KB
/
closest.xml
File metadata and controls
167 lines (157 loc) · 6.85 KB
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
161
162
163
164
165
166
167
<entries>
<entry type='method' name="closest" return="jQuery">
<signature>
<added>1.3</added>
<argument name="selector" type="Selector">
<desc>A string containing a selector expression to match elements against.</desc>
</argument>
</signature>
<signature>
<added>1.4</added>
<argument name="selector" type="Selector">
<desc>A string containing a selector expression to match elements against.</desc>
</argument>
<argument name="context" optional="true" type="Element">
<desc>A DOM element within which a matching element may be found. If no context is passed in then the context of the jQuery set will be used instead.</desc>
</argument>
</signature>
<signature>
<added>1.6</added>
<argument name="jQuery object" type="jQuery">
<desc>A jQuery object to match elements against.</desc>
</argument>
</signature>
<signature>
<added>1.6</added>
<argument name="element" type="Element">
<desc>An element to match elements against.</desc>
</argument>
</signature>
<desc>Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.</desc>
<longdesc><p>Given a jQuery object that represents a set of DOM elements, the <code>.closest()</code> method searches through these elements and their ancestors in the DOM tree and constructs a new jQuery object from the matching elements. The <code>.parents()</code> and <code>.closest()</code> methods are similar in that they both traverse up the DOM tree. The differences between the two, though subtle, are significant:</p>
<table>
<thead>
<tr>
<th>.closest()</th>
<th>.parents()</th>
</tr>
</thead>
<tbody>
<tr>
<td>Begins with the current element</td>
<td>Begins with the parent element</td></tr>
<tr>
<td>Travels up the DOM tree until it finds a match for the supplied selector</td>
<td>Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied </td>
</tr>
<tr>
<td>The returned jQuery object contains zero or one element</td>
<td>The returned jQuery object contains zero, one, or multiple elements</td>
</tr>
</tbody>
</table>
<pre>
<ul id="one" class="level-1">
<li class="item-i">I</li>
<li id="ii" class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
</pre>
<p>Suppose we perform a search for <code><ul></code> elements starting at item A:</p>
<pre>
$('li.item-a').closest('ul')
.css('background-color', 'red');
</pre>
<p>This will change the color of the level-2 <code><ul></code>, since it is the first encountered when traveling up the DOM tree.</p>
<p>Suppose we search for an <code><li></code> element instead:</p>
<pre>$('li.item-a').closest('li')
.css('background-color', 'red');
</pre>
<p>This will change the color of list item A. The <code>.closest()</code> method begins its search <em>with the element itself</em> before progressing up the DOM tree, and stops when item A matches the selector.</p>
<p>We can pass in a DOM element as the context within which to search for the closest element.</p>
<pre>var listItemII = document.getElementById('ii');
$('li.item-a').closest('ul', listItemII)
.css('background-color', 'red');
$('li.item-a').closest('#one', listItemII)
.css('background-color', 'green');</pre>
<p>This will change the color of the level-2 <code><ul></code>, because it is both the first <code><ul></code> ancestor of list item A and a descendant of list item II. It will not change the color of the level-1 <code><ul></code>, however, because it is not a descendant of list item II.</p>
</longdesc>
<example>
<desc>Show how event delegation can be done with closest. The closest list element toggles a yellow background when it or its descendent is clicked.</desc>
<code><![CDATA[
$( document ).bind("click", function( e ) {
$( e.target ).closest("li").toggleClass("hilight");
});
]]></code>
<css><![CDATA[
li { margin: 3px; padding: 3px; background: #EEEEEE; }
li.hilight { background: yellow; }
]]></css>
<html><![CDATA[<ul>
<li><b>Click me!</b></li>
<li>You can also <b>Click me!</b></li>
</ul>]]></html>
</example>
<example>
<desc>Pass a jQuery object to closest. The closest list element toggles a yellow background when it or its descendent is clicked.</desc>
<code><![CDATA[
var $listElements = $("li").css("color", "blue");
$( document ).bind("click", function( e ) {
$( e.target ).closest( $listElements ).toggleClass("hilight");
});
]]></code>
<css><![CDATA[
li { margin: 3px; padding: 3px; background: #EEEEEE; }
li.hilight { background: yellow; }
]]></css>
<html><![CDATA[<ul>
<li><b>Click me!</b></li>
<li>You can also <b>Click me!</b></li>
</ul>]]></html>
</example>
<category name="Tree Traversal" slug="tree-traversal"/>
<category name="Version 1.3" slug="1.3"/>
<category name="Version 1.4" slug="1.4"/>
<category name="Version 1.6" slug="1.6"/>
</entry>
<entry type='method' name="closest" return="Array">
<signature>
<added>1.4</added>
<deprecated>1.7</deprecated>
<argument name="selectors" type="Array">
<desc>An array or string containing a selector expression to match elements against (can also be a jQuery object).</desc>
</argument>
<argument name="context" optional="true" type="Element">
<desc>A DOM element within which a matching element may be found. If no context is passed in then the context of the jQuery set will be used instead.</desc>
</argument>
</signature>
<desc>Gets an array of all the elements and selectors matched against the current element up through the DOM tree.</desc>
<longdesc><p><strong>This signature (only!) is deprecated as of jQuery 1.7.</strong> This method is primarily meant to be used internally or by plugin authors.</p></longdesc>
<example>
<desc>Show how event delegation can be done with closest.</desc>
<code><![CDATA[
var close = $("li:first").closest(["ul", "body"]);
$.each(close, function(i){
$("li").eq(i).html( this.selector + ": " + this.elem.nodeName );
});]]></code>
<css><![CDATA[]]></css>
<html><![CDATA[<ul><li></li><li></li></ul>]]></html>
</example>
<category name="Tree Traversal" slug="tree-traversal"/>
<category name="Version 1.3" slug="1.3"/>
<category name="Version 1.4" slug="1.4"/>
<category name="Version 1.6" slug="1.6"/>
</entry>
</entries>