[英]Javascript "document.write" confusion
I'm new to JavaScript and may have asked a dumb question.. But the result returned from below code made me so confused.我是 JavaScript 新手,可能问了一个愚蠢的问题.. 但是从下面的代码返回的结果让我很困惑。 I'm not sure why only the first sentence show the value of i and it was in the last number of 3.
我不确定为什么只有第一句话显示 i 的值并且它在 3 的最后一个数字中。
I'm also confused when the code document.write('<h1>' + i + msg + '<h1>')
being executed, why it didn't just write the aggregated value of the variable 'msg'?当代码
document.write('<h1>' + i + msg + '<h1>')
被执行时,我也很困惑,为什么它不只是写变量 'msg' 的聚合值? The value of msg didn't get wiped out and it should contain both the 1st and 2nd value passed into it isn't it? msg 的值没有被清除,它应该包含传递给它的第一个和第二个值,不是吗? This looks a bit confusing to me as I came with some python knowledge and it didn't work in the same way.
这对我来说有点令人困惑,因为我掌握了一些 Python 知识,但它并没有以同样的方式工作。
var scores = [24, 32, 47]; var scores = [24, 32, 47]; var scores = [24, 32, 47]; var arrayLength = scores.length; var roundNumber = 0; var msg = ''; var i; for (i = 0; i < arrayLength; i++) { roundNumber = i + 1; msg += ' Round ' + roundNumber + ': '; msg += scores[i] + '<br>'; } document.write('<h1>' + i + msg + '<h1>')
Result returned from the html:从 html 返回的结果:
3 Round 1: 24
Round 2: 32
Round 3: 47
My expected result:我的预期结果:
0 Round 1: 24
1 Round 2: 32
2 Round 3: 47
You just need to add the i to the message in the loop instead of after, like:您只需要将 i 添加到循环中的消息而不是之后,例如:
var scores = [24, 32, 47]; var arrayLength = scores.length; var roundNumber = 0; var msg = ''; var i; for (i = 0; i < arrayLength; i++) { roundNumber = i + 1; msg += i +' Round ' + roundNumber + ': '; msg += scores[i] + '<br>'; } document.write('<h1>' + msg + '<h1>')
You output i
only once.你只输出
i
一次。 That is why you see only the last value.这就是为什么您只看到最后一个值的原因。
You need to add it to msg
inside the loop.您需要将其添加到循环内的
msg
。
var scores = [24, 32, 47]; var scores = [24, 32, 47]; var scores = [24, 32, 47]; var arrayLength = scores.length; var roundNumber = 0; var msg = ''; var i; for (i = 0; i < arrayLength; i++) { roundNumber = i + 1; msg += i + ' Round ' + roundNumber + ': '; msg += scores[i] + '<br>'; } document.write('<h1>' + msg + '<h1>')
document.write
runs after the end of the loop. document.write
在循环结束后运行。
It does output all the aggregated content of msg
.它确实输出
msg
所有聚合内容。 That isn't the issue.那不是问题。
The bit that's going wrong is the use of i
- you only use it once in the output, after the loop ends.出错的地方是
i
的使用 - 在循环结束后,您只能在输出中使用它一次。 Therefore what you see will only have the final value it had when the loop ended, hence it shows 3
, and does so once only.因此,您看到的将只有循环结束时的最终值,因此它显示
3
,并且只显示一次。
Unlike msg
which you are adding to each time the loop runs (because +=
appends new content to the existing variable), the value of i
is overwritten with a new value each time the loop runs (that's what the i++
in the for
syntax does).与每次循环运行时添加的
msg
不同(因为+=
将新内容附加到现有变量中),每次循环运行时i
的值都会被一个新值覆盖(这就是for
语法中的i++
所做的)。
To make it do what you want you simply need to incorporate the i
value into msg
as you go along instead.为了让它做你想要的,你只需要在你继续的时候将
i
值合并到msg
中。
var scores = [24, 32, 47]; var msg = ''; for (let i = 0; i < scores.length; i++) { msg += i + ' Round ' + (i+1) + ': ' + scores[i] + '<br>'; } document.write('<h1>' + msg + '<h1>')
PS I also simplified the code slightly - it's unclear why you declared the same scores
array 3 times with the same value, and arrayLength
and roundNumber
don't need to exist as separate variables, and certainly not ones which have scope outside the loop. PS 我还稍微简化了代码 - 不清楚为什么你用相同的值声明了相同的
scores
数组 3 次,并且arrayLength
和roundNumber
不需要作为单独的变量存在,当然也不需要那些在循环之外具有范围的变量。 i
also doesn't need scope outside the loop anymore. i
也不再需要循环之外的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.