forked from pandoraui/jquery-chm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheach.html
More file actions
99 lines (94 loc) · 3.91 KB
/
each.html
File metadata and controls
99 lines (94 loc) · 3.91 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>each(callback) | jQuery API 中文手册</title>
<meta name="author" content="jQuery 中文手册 - hemin.cn/jq/">
<meta name="description" content="jQuery中文手册(在线版),作者:hemin,在线手册:hemin.cn/jq/,下载:hemin.cn/jq/downloads/">
<link type="text/css" rel="stylesheet" href="style/style.css" tppabs="http://hemin.cn/jq/style/style.css" />
<script type="text/javascript" src="js/jquery.min.js" tppabs="http://hemin.cn/jq/js/jquery.min.js"></script>
<script type="text/javascript" src="js/js.js" tppabs="http://hemin.cn/jq/js/js.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-15318881-10', 'pandoraui.github.io');
ga('send', 'pageview');
</script>
</head>
<body id="split">
<div id="content" class="a2">
<div rel="each">
<h2><span>返回值:jQuery</span>each(callback)</h2>
<h3>概述</h3>
<div class="desc">
<p>以每一个匹配的元素作为上下文来执行一个函数。</p>
<div class="longdesc">
<p>意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整型)。 返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。</p>
</div>
</div>
<h3>参数</h3>
<div class="parameter">
<h4><strong>callback</strong><span>Function</span><em>V1.0</em></h4>
<p>对于每个匹配的元素所要执行的函数</p>
</div>
<div class="example">
<h3>示例</h3>
<span id="f_ad2"></span>
<h4>描述:</h4>
<p>迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。</p>
<h5>HTML 代码:</h5>
<pre><code><img/><img/></code></pre>
<h5>jQuery 代码:</h5>
<pre><code>$("img").each(function(i){
this.src = "test" + i + ".jpg";
});</code></pre>
<h5>结果:</h5>
<pre><code>[ <img src="test0.jpg" />, <img src="test1.jpg" /> ]</code></pre>
<h4>描述:</h4>
<p>如果你想得到 jQuery对象,可以使用 $(this) 函数。</p>
<h5>HTML 代码:</h5>
<pre><code><button>Change colors</button>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div>
<div></div>
<div></div></code></pre>
<h5>jQuery 代码:</h5>
<pre><code>$("img").each(function(){
$(this).toggleClass("example");
});</code></pre>
<h4>描述:</h4>
<p>你可以使用 'return' 来提前跳出 each() 循环。</p>
<h5>HTML 代码:</h5>
<pre><code><button>Change colors</button>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div id="stop">Stop here</div>
<div></div>
<div></div>
<div></div></code></pre>
<h5>jQuery 代码:</h5>
<pre><code>$("button").click(function () {
$("div").each(function (index, domEle) {
// domEle == this
$(domEle).css("backgroundColor", "yellow");
if ($(this).is("#stop")) {
$("span").text("Stopped at div index #" + index);
return false;
}
});
});</code></pre>
</div>
</div>
</div>
</body>
</html>