javascript简单的while循环不添加到innerHtml

发布于 2024-12-11 09:56:10 字数 1338 浏览 0 评论 0原文

好吧,这是我的愚蠢问题。我在这里学习……我已经研究这个有一段时间了,但我不明白。我有一个数组,其中包含一些通过对象提供的值。像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卷耳 2024-12-18 09:56:10

在每次迭代中,您都会覆盖(重新分配)'showMore'innerHtml 值,

请改用 += 附加(添加到)'showMore'当前现有内容

document.getElementById('showMore').innerHTML += mssgs;

旁注

我还希望您使用for 在这种情况下循环,因为您正在循环遍历静态数组,其中需要迭代的次数从一开始就已知

(<代码>blogRun.length )

for ( var i = 0, len = blogRun.length; i < len; i++ ) {
    // .... do your stuff here.
}

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'

document.getElementById('showMore').innerHTML += mssgs;

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 )

for ( var i = 0, len = blogRun.length; i < len; i++ ) {
    // .... do your stuff here.
}
垂暮老矣 2024-12-18 09:56:10

innerHTML 会替换元素的全部内容。

这应该效果更好

//before the while loop
var strHTML= '';
...
//inside while loop
strHTML += mssgs;
...
//after while loop
document.getElementById('showMore').innerHTML = strHTML;

innerHTML replaces the full content of your element.

this should work better

//before the while loop
var strHTML= '';
...
//inside while loop
strHTML += mssgs;
...
//after while loop
document.getElementById('showMore').innerHTML = strHTML;
与君绝 2024-12-18 09:56:10

.innerHtml 将在每个循环中替换元素 showMore 中的值。如果您希望 showMore 显示所有值,您需要将这些值附加/连接在一起,然后替换innerHtml。

或者您应该能够简单地将 mssgs 值附加到innerHtml:

document.getElementById('showMore').innerHTML += mssgs;

.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:

document.getElementById('showMore').innerHTML += mssgs;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文