javascript简单的while循环不添加到innerHtml
好吧,这是我的愚蠢问题。我在这里学习……我已经研究这个有一段时间了,但我不明白。我有一个数组,其中包含一些通过对象提供的值。像这样:
function blogObj(name,date,user_entry){
this.names = name;
this.dates = date;
this.blog_entry = user_entry;
this.propsComb = this.names + this.dates + this.blog_entry;
}
var blogRun = [
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
];
var i=0;
var mssgs = "";
现在,当我像这样用这个 doc.write 来测试它时:
function hmm(){
while(i < blogRun.length){
mssgs = blogRun[i].propsComb + '<br/>';
document.write(mssgs);
i++;
}
}
我得到了我需要的所有值,这些值根据循环和数组的值命名为 x5 。所以本质上它是有效的。
现在,当我将 "document.write(mssgs)"
替换为
document.getElementById('showMore').innerHTML = mssgs;
它时,
function hmm(){
while(i < blogRun.length){
mssgs = blogRun[i].propsComb + '<br/>';
//document.write(mssgs);
document.getElementById('showMore').innerHTML = mssgs;
i++;
}
}
它只显示数组的最后一个值。它不会循环遍历数组的其余值。 我所做的就是用 getElementById 等替换 .write 。
Ok so this is my dumb problem. im here learning and...ive been looking at this for a while and i dont understand. i have an array with some values that are fed via an object. like so:
function blogObj(name,date,user_entry){
this.names = name;
this.dates = date;
this.blog_entry = user_entry;
this.propsComb = this.names + this.dates + this.blog_entry;
}
var blogRun = [
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
new blogObj("name ", "something ", "something "),
];
var i=0;
var mssgs = "";
now when i loop through it like this with this doc.write to test:
function hmm(){
while(i < blogRun.length){
mssgs = blogRun[i].propsComb + '<br/>';
document.write(mssgs);
i++;
}
}
i get all the values i need which are name something something x5 as per the loop and the values of the array. so essentially it works.
now when i replace the "document.write(mssgs)"
with
document.getElementById('showMore').innerHTML = mssgs;
making it this
function hmm(){
while(i < blogRun.length){
mssgs = blogRun[i].propsComb + '<br/>';
//document.write(mssgs);
document.getElementById('showMore').innerHTML = mssgs;
i++;
}
}
it only shows me the last value of the array. it doesnt loop for the rest of the arrays values.
all im doing is replacing the .write with getElementById etc..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每次迭代中,您都会覆盖(重新分配)
'showMore'
的innerHtml
值,请改用
+=
附加(添加到)'showMore'
当前现有内容旁注
我还希望您使用
for
在这种情况下循环,因为您正在循环遍历静态数组,其中需要迭代的次数从一开始就已知(<代码>blogRun.length )
At each iteration you are overwriting (re-assigning) the
innerHtml
value of'showMore'
use
+=
instead to append (add to) the currently existing contents of'showMore'
Side Note
I would also prefer that you use a
for
loop in this situation as you are looping over a static array where the amount of times you need to iterate is known from the beginning(
blogRun.length
)innerHTML 会替换元素的全部内容。
这应该效果更好
innerHTML replaces the full content of your element.
this should work better
.innerHtml 将在每个循环中替换元素 showMore 中的值。如果您希望 showMore 显示所有值,您需要将这些值附加/连接在一起,然后替换innerHtml。
或者您应该能够简单地将 mssgs 值附加到innerHtml:
.innerHtml will replace the value within the element showMore on each loop. If you want showMore to display all of the values you need to append / concatenate the values together and then replace innerHtml.
Or you should be able to simply append the mssgs value to the innerHtml: