<!DOCTYPE html> <html> <body> <p>只要 i 小于 5,点击按钮就可以循环遍历一段代码。</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++; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>