【问题标题】:i want to print the value of "i" from 0 to 1000我想打印从 0 到 1000 的“i”值
【发布时间】:2021-04-04 17:52:56
【问题描述】:
<!DOCTYPE html>
<html>
<body>
/*how ican make String.fromCharCode(i)

我取值从 0 到 1000 所以每次都打印出字母 */

JavaScript 循环

<p id="demo"></p>

<script>
var text = "";
var i;
for (i = 0; i < 1000; i++) {
/*String.fromCharCode(i) "i" print the letter only 1000 
and i want to print from 0 to 1000 */
text = String.fromCharCode(i)+ "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

【问题讨论】:

  • 因为您在每次迭代时都会完全重新分配 text 的值。相反,使用text += String...追加到前一个值
  • 非常感谢它,现在可以工作了

标签: javascript loops for-loop charat


【解决方案1】:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>

<p id="demo"></p>

<script>
let text = "";
for (let i = 0; i < 1000; i++) {
  text += 'i' + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2020-10-04
    • 2021-11-29
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多