1. JavaScript for/in 语句循环遍历对象的属性:
可以遍历数组,也可以遍历一个对象的所有属性
<body> <p>点击按钮,循环遍历对象“person”的属性</p> <button onclick="myfunction()">点击这里</button> <p id="d1"></p> <script> function myfunction() { var txt = ""; var person = { firstname: "yoyo", lastname: "xiao", age: 34 }; for(x in person) { txt = txt + person[x];//把属性的值都转入一个数组 } document.getElementById("d1").innerHTML = txt; } </script> <script> var cars = ["Audi", "BMW", "Saab","Ford"]; for (x in cars) { document.write(cars[x]+"</br>"); } </script> </body>
2. For循环
for (语句 1; 语句 2; 语句 3) { 被执行的代码块 }
3. While循环
while(condition)
{
代码块
}
do
{
} while(condition)
4. break: 跳出循环
continue: 跳出当次循环