<script> <!DOCTYPE html> <html> <head> <metacharset="utf-8"> </head> <body> <p>点击按钮循环代码块 5 次。</p> <buttononclick="test()">点我</button> <pid="demo"></p> <script> var text = ""; functiontest() { for (let i = 0; i < 5; i++) { text += `The number is ${i}<br>`; } /* // 使用 continue 语句,在i等于3时跳过当前循环 for (i = 0; i < 5; i++) { if (i == 3) { continue; } text += `The number is ${i}<br>`; } */ /* // 使用 break 语句 在i等于3时跳出当前循环 for (i = 0; i < 5; i++) { if (i == 3) { break; } text += `The number is ${i}<br>`; } */ document.getElementById("demo").innerHTML = text; } </script>
<script> var person = { name: "quanyi", position: "frontend devloper", age: 25 }; var text = ""; functiontest() { for (let x in person) { text += person[x] + " <br>"; console.log(x); } document.getElementById("demo").innerHTML = text; }
</script>
</body>
</html>
for of 循环
遍历字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!DOCTYPE html> <html> <head> <metacharset="utf-8"> </head> <body> <script> var string="why give up treatment"; for(let i of string){ console.log(i); } </script> </body> </html>