【发布时间】:2017-10-13 05:51:58
【问题描述】:
我已经尝试了大约 3 个小时来解决这个问题,但出现了多次失败,而我的代码最终只是回到了这一点。
<html>
<head>
<title>Learning</title>
<script type="text/javascript">
function theMath(userOutput) {
document.getElementById("userOutput").innerHTML = userOutput
}
</script>
</head>
<body>
<p id="userOutput"></p>
<p> Counting from one to ten</p>
<input type="button" id="btnOutput" value="Add a number!" />
<p></p>
<script>
var strOutput = "";
var i;
for (i = 0; i <= 10; i++) {
strOutput = " " + i;
document.write(strOutput)
}
</script>
</body>
</html>
目前所有的数字都出现在按钮下,但是我需要做的是,当我点击 btnOutput 时,会显示 for 循环的第一个数字 (0)。当我再次单击 btnOutput 时,将显示 for 循环的第二个数字 (1)。 所以每次我点击 btnOutput 循环都会运行一次,直到它不能再运行(当 i=11 时)
没有提到:我正在做的事情需要一个 for 循环。
【问题讨论】:
标签: javascript html loops for-loop button