<html>
<body>
<p>单击该按钮可循环遍历拥有四个元素的数组的索引,但当 i 等于“3”时退出循环。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i;
for (i = 0;; i++) {
if (i == 3) {
break;
}
text += cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>