【代码笔记】Web-JavaScript-javaScript for循环
一,效果图。
二,代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript for循环</title>
</head>
<body>
<script>
cars = ["bmw", "volvo", "saab", "ford"];
for (var i = 0; i < cars.length; i++) {
document.write(cars[i] + "<br>");
}
</script>
<p>点击按钮循环代码5次</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
for (var i = 0; i < 5; i++) {
x = x + "the number is" + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
<p>点击下面的按钮,循环遍历对象"person"的属性</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo1"></p>
<script>
function myFunction() {
var x;
var txt = "";
var person = {
fname: "bill",
lname: "gates",
age: 56
};
for (x in person) {
txt = txt + person[x];
}
document.getElementById("demo1").innerHTML = txt;
}
</script>
</body>
</html>
参考资料:《菜鸟教程》
本文来自博客园,作者:花儿迎风笑,转载请注明原文链接:https://www.cnblogs.com/yang-guang-girl/p/10147401.html