<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>