【发布时间】:2013-12-28 21:40:57
【问题描述】:
当我在浏览器中运行以下代码时。
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of code five times.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x="";
for (var i=0;i<5;i++)
{
x = x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
显示如下
数量为 0 数量为 1 数量为 2 数量是 3 数量是 4 点击“试用”按钮下方。我的问题是这部分代码x=x + "The number is " + i + "<br>";。当它被替换为x="The number is " + i + "<br>"; 时,它只显示 数字是 4。有些人请解释为什么会这样。
【问题讨论】:
-
x += 'The number is '+ i +'<br>' -
通过一个好的 javascript 教程你会得到更好的帮助。顺便问一下,这是作业吗?
标签: javascript html dom