循环错误的Javascript
Javascript for-loop error
例如,我有以下代码:
<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>
我的问题是,为什么x=x+"The number is " + i + "<br>";
而不是x="The number is " + i + "<br>";
它将一个字符串附加到另一个字符串,构建更长的字符串。
所以在第一次迭代之后,x
等于"The number is 0<br>"
,在第二次迭代之后x
的值是"The number is 0<br>The number is 1<br>"
。等等。
第一个代码片段x=x+"The number is " + i + "<br>";
将每个新消息附加到字符串x
的末尾;第二个CCD_ 9简单地用新消息替换x。
据推测,使用第一行是为了同时显示所有输出,而不是只显示最后一行。
One(x=x+"The number is " + i + "<br>";
)将把输出附加到x
并输出:
The number is 0<br>
The number is 1<br>
The number is 2<br>
The number is 3<br>
The number is 4<br>
另一个(x="The number is " + i + "<br>";
)将在每次迭代和输出时取代x
:
The number is 4<br>
When you use :-
x=x+"The number is " + i + "<br>"
It will print whole series numbers
and When you use :-
x="The number is " + i + "<br>"
It will print the last value of your series.
相关文章:
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- 将字符串拆分为数组时出现JavaScript错误
- 处理JSON字符串会导致JavaScript错误
- 使用AJAX在剑道网格中分组时出现Javascript错误
- Ajax对web URL的调用导致javascript错误
- 为什么当我尝试在Ionic中使用列表时会出现此JavaScript错误
- 可以't解决Javascript错误
- django出现神秘的javascript错误
- JavaScript错误无效的限定符
- 使用主题运行时portlet liferay时出现Javascript错误
- 绑定函数时在IE7中未实现Javascript错误
- Javascript错误:未捕获引用错误:未定义编辑
- eventListener出现JavaScript错误
- sharepoint站点的internet explorer中出现Javascript错误
- LoadControl HttpCompileException自定义服务器控件JavaScript错误
- 销毁Bootstrap popover时出现Javascript错误
- 无法加载Javascript错误XMLHttpRequest
- 使用JSON.parse时出现无效字符javascript错误
- 修复Javascript错误
- 模板字符串上的netbeans javascript错误(backticks)