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/each.xml
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -31,22 +31,22 @@ $( "li" ).each(function( index ) {
31
31
</p>
32
32
<p>You can stop the loop from within the callback function by returning <code>false</code>.</p>
33
33
<p>Note: most jQuery methods that return a jQuery object also loop through the set of elements in the jQuery collection — a process known as <i>implicit iteration</i>. When this occurs, it is often unnecessary to <i>explicitly</i> iterate with the <code>.each()</code> method:</p>
34
-
<pre><code>
34
+
<pre><code>
35
35
// The .each() method is unnecessary here:
36
36
$( "li" ).each(function() {
37
37
$( this ).addClass( "foo" );
38
38
});
39
39
40
40
// Instead, you should rely on implicit iteration:
41
41
$( "li" ).addClass( "bar" );
42
-
</code></pre>
42
+
</code></pre>
43
43
</longdesc>
44
44
<example>
45
45
<desc>Iterate over three divs and sets their color property.</desc>
46
46
<code><![CDATA[
47
47
$( document.body ).click(function() {
48
48
$( "div" ).each(function( i ) {
49
-
if ( this.style.color != "blue" ) {
49
+
if ( this.style.color !== "blue" ) {
50
50
this.style.color = "blue";
51
51
} else {
52
52
this.style.color = "";
@@ -102,12 +102,12 @@ To do list: <span>(click here to change)</span>
102
102
]]></html>
103
103
</example>
104
104
<example>
105
-
<desc>Use "return" to break out of each() loops early.</desc>
105
+
<desc>Use <code>return false</code> to break out of each() loops early.</desc>
106
106
<code><![CDATA[
107
107
$( "button" ).click(function() {
108
-
$( "div" ).each(function( index, domEle ) {
109
-
// domEle == this
110
-
$( domEle ).css( "backgroundColor", "yellow" );
108
+
$( "div" ).each(function( index, element ) {
109
+
// element == this
110
+
$( element ).css( "backgroundColor", "yellow" );
111
111
if ( $( this ).is( "#stop" ) ) {
112
112
$( "span" ).text( "Stopped at div index #" + index );
Copy file name to clipboardExpand all lines: entries/empty.xml
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ $( ".hello" ).empty();
24
24
<div class="goodbye">Goodbye</div>
25
25
</div>
26
26
</code></pre>
27
-
<p>If we had any number of nested elements inside <code><div class="hello"></code>, they would be removed, too.</p>
27
+
<p>If we had any number of nested elements inside <code><div class="hello"></code>, they would be removed, too.</p>
28
28
<p>To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.</p>
29
29
<p>If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use <ahref="/detach/"><code>.detach()</code></a> instead.</p>
Copy file name to clipboardExpand all lines: entries/end.xml
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -19,15 +19,15 @@
19
19
<li>list item 2</li>
20
20
<li class="bar">list item 3</li>
21
21
</ul>
22
-
</code></pre>
22
+
</code></pre>
23
23
<p>The <code>end()</code> method is useful primarily when exploiting jQuery's chaining properties. When not using chaining, we can usually just call up a previous object by variable name, so we don't need to manipulate the stack. With <code>end()</code>, though, we can string all the method calls together:</p>
24
24
<pre><code>
25
25
$( "ul.first" )
26
26
.find( ".foo" )
27
27
.css( "background-color", "red" )
28
28
.end()
29
-
.find( ".bar" )
30
-
.css( "background-color", "green" );
29
+
.find( ".bar" )
30
+
.css( "background-color", "green" );
31
31
</code></pre>
32
32
<p>This chain searches for items with the class <code>foo</code> within the first list only and turns their backgrounds red. Then <code>end()</code> returns the object to its state before the call to <code>find()</code>, so the second <code>find()</code> looks for '.bar' inside <code><ul class="first"></code>, not just inside that list's <code><li class="foo"></code>, and turns the matching elements' backgrounds green. The net result is that items 1 and 3 of the first list have a colored background, and none of the items from the second list do.</p>
33
33
<p>A long jQuery chain can be visualized as a structured code block, with filtering methods providing the openings of nested blocks and <code>end()</code> methods closing them:</p>
@@ -50,7 +50,7 @@ jQuery.fn.showTags = function( n ) {
<desc>Apply three different styles to list items to demonstrate that <code>:eq()</code> is designed to select a single element while <code>:nth-child()</code> or <code>:eq()</code> within a looping construct such as <code>.each()</code> can select multiple elements.</desc>
41
41
<code><![CDATA[
42
42
// Applies yellow background color to a single <li>
0 commit comments