梦入琼楼寒有月,行过石树冻无烟

JavaScript for 循环

循环5次(for……)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button onclick="myFunction()">开始</button>
<p id="data"></p>
<script>
function myFunction() {
var a = "";
for (var i=0;i<10;i++) {
a = a + "本次执行 " + i + "<br>";
}
document.getElementById('data').innerHTML = a;
}
</script>
</body>
</html>

遍历对象 (for……in)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button onclick="myFunction()">开始</button>
<p id="data"></p>
<script>
function myFunction() {
var x;
var two = "";
var person = {one:"ones",
two:"twos",
three:"three"};
for (x in person) {
two = two + person[x];
}
document.getElementById('data').innerHTML = two;
}
</script>
</body>
</html>
⬅️ Go back