You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: entries/add.xml
+79-54
Original file line number
Diff line number
Diff line change
@@ -36,97 +36,122 @@
36
36
</signature>
37
37
<desc>Add elements to the set of matched elements.</desc>
38
38
<longdesc>
39
-
<p>Given a jQuery object that represents a set of DOM elements, the <code>.add()</code> method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to <code>.add()</code> can be pretty much anything that <code>$()</code> accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.</p>
39
+
<p>Given a jQuery object that represents a set of DOM elements, the <code>.add()</code> method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to <code>.add()</code> can be pretty much anything that <code>$()</code> accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.</p>
40
40
<p>Do not assume that this method appends the elements to the existing collection in the order they are passed to the <code>.add()</code> method. When all elements are members of the same document, the resulting collection from <code>.add()</code> will be sorted in document order; that is, in order of each element's appearance in the document. If the collection consists of elements from different documents or ones not in any document, the sort order is undefined. To create a jQuery object with elements in a well-defined order, use the <code>$(array_of_DOM_elements)</code> signature.</p>
41
41
<p>The updated set of elements can be used in a following (chained) method, or assigned to a variable for later use. For example:</p>
42
-
<pre>
43
-
<code>
44
-
$("p").add("div").addClass("widget");
45
-
var pdiv = $("p").add("div");
46
-
</code>
47
-
</pre>
42
+
<pre><code>
43
+
$( "p" ).add( "div" ).addClass( "widget" );
44
+
var pdiv = $( "p" ).add( "div" );
45
+
</code></pre>
48
46
<p>The following will <em>not</em> save the added elements, because the <code>.add()</code> method creates a new set and leaves the original set in pdiv unchanged:</p>
49
-
<pre>
50
-
<code>
51
-
var pdiv = $("p");
52
-
pdiv.add("div"); // WRONG, pdiv will not change
53
-
</code>
54
-
</pre>
47
+
<pre><code>
48
+
var pdiv = $( "p" );
49
+
pdiv.add( "div" ); // WRONG, pdiv will not change
50
+
</code></pre>
55
51
<p>Consider a page with a simple list and a paragraph following it:</p>
56
-
<pre>
57
-
<code><ul>
52
+
<pre><code>
53
+
<ul>
58
54
<li>list item 1</li>
59
55
<li>list item 2</li>
60
56
<li>list item 3</li>
61
57
</ul>
62
-
<p>a paragraph</p></code>
63
-
</pre>
58
+
<p>a paragraph</p>
59
+
</code></pre>
64
60
<p>We can select the list items and then the paragraph by using either a selector or a reference to the DOM element itself as the <code>.add()</code> method's argument:</p>
<p>The result of this call is a red background behind all four elements.
74
68
Using an HTML snippet as the <code>.add()</code> method's argument (as in the third version), we can create additional elements on the fly and add those elements to the matched set of elements. Let's say, for example, that we want to alter the background of the list items along with a newly created paragraph:</p>
<p>Although the new paragraph has been created and its background color changed, it still does not appear on the page. To place it on the page, we could add one of the insertion methods to the chain.</p>
80
74
<p>As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).</p>
81
75
<p><strong>Note:</strong> To reverse the <code>.add()</code> you can use <ahref="http://api.jquery.com/not"><code>.not( elements | selector )</code></a> to remove elements from the jQuery results, or <ahref="http://api.jquery.com/end"><code>.end()</code></a> to return to the selection before you added.</p>
82
76
</longdesc>
83
77
<example>
84
78
<desc>Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.</desc>
85
79
<code><![CDATA[
86
-
87
-
$("div").css("border", "2px solid red")
88
-
.add("p")
89
-
.css("background", "yellow");
80
+
$( "div" ).css( "border", "2px solid red" )
81
+
.add( "p" )
82
+
.css( "background", "yellow" );
90
83
]]></code>
91
84
<css><![CDATA[
92
-
div { width:60px; height:60px; margin:10px; float:left; }
93
-
p { clear:left; font-weight:bold; font-size:16px;
94
-
color:blue; margin:0 10px; padding:2px; }
95
-
]]></css>
96
-
<html><![CDATA[<div></div>
85
+
div {
86
+
width: 60px;
87
+
height: 60px;
88
+
margin: 10px;
89
+
float: left;
90
+
}
91
+
p {
92
+
clear: left;
93
+
font-weight: bold;
94
+
font-size: 16px;
95
+
color: blue;
96
+
margin: 0 10px;
97
+
padding: 2px;
98
+
}
99
+
]]></css>
100
+
<html><![CDATA[
101
+
<div></div>
97
102
98
-
<div></div>
99
-
<div></div>
100
-
<div></div>
101
-
<div></div>
102
-
<div></div>
103
+
<div></div>
104
+
<div></div>
105
+
<div></div>
106
+
<div></div>
107
+
<div></div>
103
108
104
-
<p>Added this... (notice no border)</p>]]></html>
109
+
<p>Added this... (notice no border)</p>
110
+
]]></html>
105
111
</example>
106
112
<example>
107
113
<desc>Adds more elements, matched by the given expression, to the set of matched elements.</desc>
<p>First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to <code>.nextAll()</code> then pushes the set of items 4 and 5 onto the stack. Finally, the <code>.addBack()</code> invocation merges these two sets together, creating a jQuery object that points to all three items in document order: <code>{[<li.third-item>,<li>,<li> ]}</code>.</p>
28
28
</longdesc>
29
29
<example>
30
30
<desc>The <code>.addBack()</code> method causes the previous set of DOM elements in the traversal stack to be added to the current set. In the first example, the top stack contains the set resulting from <code>.find("p")</code>. In the second example, <code>.addBack()</code> adds the previous set of elements on the stack — in this case <code>$("div.after-addback")</code> — to the current set, selecting both the div and its enclosed paragraphs.</desc>
31
31
<code><![CDATA[
32
-
$("div.left, div.right").find("div, div > p").addClass("border");
32
+
$("div.left, div.right").find("div, div > p").addClass("border");
Copy file name to clipboardExpand all lines: entries/addClass.xml
+60-32
Original file line number
Diff line number
Diff line change
@@ -17,64 +17,92 @@
17
17
<longdesc>
18
18
<p>It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.</p>
19
19
<p>More than one class may be added at a time, separated by a space, to the set of matched elements, like so:</p>
<p>Here, the <code>myClass</code> and <code>noClass</code> classes are removed from all paragraphs, while <code>yourClass</code> is added.</p>
24
28
<p>As of jQuery 1.4, the <code>.addClass()</code> method's argument can receive a function.</p>
25
-
<pre><code>$("ul li").addClass(function(index) {
29
+
<pre><code>
30
+
$( "ul li" ).addClass(function( index ) {
26
31
return "item-" + index;
27
-
});</code></pre>
32
+
});
33
+
</code></pre>
28
34
<p>Given an unordered list with two <code><li></code> elements, this example adds the class "item-0" to the first <code><li></code> and "item-1" to the second.</p>
29
35
</longdesc>
30
36
<example>
31
37
<desc>Add the class "selected" to the matched elements.</desc>
32
38
<code><![CDATA[
33
-
$("p").last().addClass("selected");
34
-
]]></code>
39
+
$( "p").last().addClass("selected");
40
+
]]></code>
35
41
<css><![CDATA[
36
-
p { margin: 8px; font-size:16px; }
37
-
.selected { color:blue; }
38
-
.highlight { background:yellow; }
42
+
p {
43
+
margin: 8px;
44
+
font-size:16px;
45
+
}
46
+
.selected {
47
+
color:blue;
48
+
}
49
+
.highlight {
50
+
background:yellow;
51
+
}
39
52
]]></css>
40
53
<html><![CDATA[
41
-
<p>Hello</p>
42
-
<p>and</p>
43
-
<p>Goodbye</p>
44
-
]]></html>
54
+
<p>Hello</p>
55
+
<p>and</p>
56
+
<p>Goodbye</p>
57
+
]]></html>
45
58
</example>
46
59
<example>
47
60
<desc>Add the classes "selected" and "highlight" to the matched elements.</desc>
48
61
<code><![CDATA[
49
-
$("p:last").addClass("selected highlight");
50
-
]]></code>
62
+
$( "p:last").addClass("selected highlight");
63
+
]]></code>
51
64
<css><![CDATA[
52
-
p { margin: 8px; font-size:16px; }
53
-
.selected { color:red; }
54
-
.highlight { background:yellow; }
65
+
p {
66
+
margin: 8px;
67
+
font-size: 16px;
68
+
}
69
+
.selected {
70
+
color: red;
71
+
}
72
+
.highlight {
73
+
background: yellow;
74
+
}
55
75
]]></css>
56
-
<html><![CDATA[<p>Hello</p>
57
-
<p>and</p>
58
-
<p>Goodbye</p>]]></html>
76
+
<html><![CDATA[
77
+
<p>Hello</p>
78
+
<p>and</p>
79
+
<p>Goodbye</p>
80
+
]]></html>
59
81
</example>
60
82
<example>
61
83
<desc>Pass in a function to <code>.addClass()</code> to add the "green" class to a div that already has a "red" class.</desc>
0 commit comments