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
+31-22
Original file line number
Diff line number
Diff line change
@@ -10,31 +10,40 @@
10
10
<desc>Iterate over a jQuery object, executing a function for each matched element. </desc>
11
11
<longdesc>
12
12
<p>The <code>.each()</code> method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword <code>this</code> refers to the element.</p>
13
-
<p>Suppose we had a simple unordered list on the page:</p>
13
+
<p>Suppose you have a simple unordered list on the page:</p>
14
14
<pre><code><ul>
15
15
<li>foo</li>
16
16
<li>bar</li>
17
17
</ul>
18
18
</code></pre>
19
-
<p>We can select the list items and iterate across them:</p>
20
-
<pre><code>$('li').each(function(index) {
21
-
alert(index + ': ' + $(this).text());
19
+
<p>You can select the list items and iterate across them:</p>
20
+
<pre><code>$( "li" ).each(function(index) {
21
+
console.log( index + ": "" + $(this).text());
22
22
});
23
23
</code></pre>
24
-
<p>A message is thus alerted for each item in the list:</p>
24
+
<p>A message is thus logged for each item in the list:</p>
25
25
<p>
26
26
<spanclass="output">0: foo</span>
27
27
<br/>
28
28
<spanclass="output">1: bar</span>
29
29
</p>
30
-
<p>We can stop the loop from within the callback function by returning <code>false</code>.</p>
30
+
<p>You can stop the loop from within the callback function by returning <code>false</code>.</p>
31
+
<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>
32
+
<pre><code>// The .each() method is unnecessary here:
33
+
$( "li" ).each(function() {
34
+
$(this).addClass( "foo" );
35
+
});
36
+
37
+
// Instead, you should rely on implicit iteration:
38
+
$( "li" ).addClass( "bar" );
39
+
</code></pre>
31
40
</longdesc>
32
41
<example>
33
-
<desc>Iterates over three divs and sets their color property.</desc>
42
+
<desc>Iterate over three divs and sets their color property.</desc>
34
43
<code><![CDATA[
35
44
$(document.body).click(function () {
36
-
$("div").each(function (i) {
37
-
if (this.style.color != "blue") {
45
+
$("div").each(function (i) {
46
+
if (this.style.color != "blue") {
38
47
this.style.color = "blue";
39
48
} else {
40
49
this.style.color = "";
@@ -43,19 +52,19 @@
43
52
});
44
53
]]></code>
45
54
<css><![CDATA[
46
-
div { color:red; text-align:center; cursor:pointer;
55
+
div { color:red; text-align:center; cursor:pointer;
47
56
font-weight:bolder; width:300px; }
48
57
]]></css>
49
58
<html><![CDATA[<div>Click here</div>
50
59
<div>to iterate through</div>
51
60
<div>these divs.</div>]]></html>
52
61
</example>
53
62
<example>
54
-
<desc>If you want to have the jQuery object instead of the regular DOM element, use the $(this) function, for example:</desc>
63
+
<desc>To access a jQuery object instead of the regular DOM element, use <code>$(this)</code>. For example:</desc>
55
64
<code><![CDATA[
56
-
$("span").click(function () {
57
-
$("li").each(function(){
58
-
$(this).toggleClass("example");
65
+
$("span").click(function () {
66
+
$("li").each(function(){
67
+
$(this).toggleClass("example");
59
68
});
60
69
});
61
70
@@ -74,14 +83,14 @@
74
83
</ul>]]></html>
75
84
</example>
76
85
<example>
77
-
<desc>You can use 'return' to break out of each() loops early.</desc>
86
+
<desc>Use "return" to break out of each() loops early.</desc>
0 commit comments