<html>
<body>
<p>点击按钮执行循环,并在 i 等于 “3” 的时候跳出循环。</p>
<button onclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i = 0;
while (i < 5) {
text += "<br>The number is " + i;
i++;
if (i == 3) {
break;
}
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>