Skip to content

Commit 0f68a98

Browse files
committed
each方法的应用
1 parent 7072763 commit 0f68a98

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
6+
<title></title>
7+
<style>
8+
p {
9+
color: red;
10+
}
11+
div{
12+
width:200px;
13+
height: 100px;
14+
background-color: yellow;
15+
color:red;
16+
}
17+
</style>
18+
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
19+
</head>
20+
21+
<body>
22+
<h2>each方法</h2>
23+
<p>慕课网,专注分享</p>
24+
<div id="aaron"></div>
25+
点击观察结果:
26+
<select id="animation">
27+
<option value="1">each数组</option>
28+
<option value="2">each对象</option>
29+
</select>
30+
<input id="exec" type="button" value="执行动画">
31+
<script type="text/javascript">
32+
$("#exec").click(function() {
33+
var v = $("#animation").val();
34+
var $aaron = $("#aaron");
35+
$aaron.empty();
36+
if (v == "1") {
37+
38+
// 遍历数组元素
39+
$.each(['Aaron', '慕课网'], function(i, item) {
40+
$aaron.append("索引=" + i + "; 元素=" + item);
41+
});
42+
} else if (v == "2") {
43+
// 遍历对象属性
44+
$.each({
45+
name: "张三",
46+
age: 18
47+
}, function(property, value) {
48+
$aaron.append("属性名=" + property + "; 属性值=" + value);
49+
});
50+
}
51+
});
52+
</script>
53+
</body>
54+
55+
</html>

0 commit comments

Comments
 (0)