前辈,前两种输出“0,1,2,3,4”晚辈认同,但第三种不是应该输出“4”吗,为何输出的也是“0,1,2,3,4”??
第一种
<body>
<p>点击下面的按钮,只要i小于5就一直循环代码块。</p>
<button onclick="fun()">请点击这里</button>
<p id="demo"></p>
<script>
function fun()
{
var x="",i=0;
do{
x=x+"The number is "+i+".<br />";
// document.getElementById("demo").innerHTML=x;
i++;
}while(i<5) document.getElementById("demo").innerHTML=x;
}
</script>
</body>
第二种
<body>
<p>点击下面的按钮,只要i小于5就一直循环代码块。</p>
<button onclick="fun()">请点击这里</button>
<p id="demo"></p>
<script>
function fun()
{
var x="",i=0;
do{
x=x+"The number is "+i+".<br />";
document.getElementById("demo").innerHTML=x;
i++;
}while(i<5);
// document.getElementById("demo").innerHTML=x;
}
</script>
</body>
第三种
<body>
<p>点击下面的按钮,只要i小于5就一直循环代码块。</p>
<button onclick="fun()">请点击这里</button>
<p id="demo"></p>
<script>
function fun()
{
var x="",i=0;
do{
x=x+"The number is "+i+".<br />";
// document.getElementById("demo").innerHTML=x;
i++;
}while(i<5);
document.getElementById("demo").innerHTML=x;
}
</script>
</body>