一,效果圖。 二,代碼。 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>javascript break和continue語句</title> </head> <body> <!--break語句--> <p>點擊按鈕,測試帶 ...
一,效果圖。
二,代碼。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript break和continue語句</title>
</head>
<body>
<!--break語句-->
<p>點擊按鈕,測試帶有brak語句的迴圈</p>
<button onclick="myFunction()">點擊這裡</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
for (i = 0; i < 10; i++) {
if (i == 3) {
break;
}
x = x + "the number is" + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
<!--continue語句-->
<p>點擊下麵按鈕來執行迴圈,該迴圈會跳過i=3的步進</p>
<button onclick="myFunction()">點擊這裡</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "",
i = 0;
for (i = 0; i < 10; i++) {
if (i == 3) {
continue;
}
x = x + "the number is" + i + "<br>";
}
document.getElementById("demo").innerHTML = x;
}
</script>
<!--javascript標簽-->
<script>
cars = ["BMW", "volvo", "Saab", "Ford"];
list: {
document.write(cars[0] + "<br>");
document.write(cars[1] + "<br>");
document.write(cars[2] + "<br>");
break list;
document.write(cars[3] + "<br>");
document.write(cars[4] + "<br>");
document.write(cars[5] + "<br>");
}
</script>
</body>
</html>
參考資料:《菜鳥教程》