将循环结果输出到 .innerHTML

Output the results of loops to .innerHTML

我看了其他类似问题的答案,要么是我的大脑太累了,要么 javascript 太复杂了,我无法理解,或者我太笨了。我很新,到目前为止了解有限。 我试图理解循环并希望将循环的输出输出到我的 div 容器,但是当我这样做时,它们只有 运行 一个循环实例(大概是最后一次迭代)而不是像在控制台中那样显示整个内容。下面是两个例子:

var x = 0;
while (x < 5) {
    console.log("Hello World")
      // document.getElementById('output').innerHTML = "Hello World";
      // Why wont it output this 5 times but will log to the console 5 times?
    x++
  }

// For Loop

for (var i = 0; i < 5; i++)
{

console.log("Hello World with 'for loop'");
  // document.getElementById('output').innerHTML = "Hello World";
  // Same with this one...
  }

换成这个就可以了

document.getElementById('output').innerHTML += "hello world"