简体   繁体   English

InnerHTML会覆盖以前的条目吗?

[英]Does InnerHTML overwrite previous entries?

As a frist year student we just learned how to use DOM instead of document.write to display our elements. 作为一名学生,我们刚刚学会了如何使用DOM而不是document.write来显示我们的元素。

I have a loop and i want it to display multiple elements after it goes through all of them but it always displays the last one so i am guessing the previous entries are being overwritten? 我有一个循环,我希望它在经过所有这些后显示多个元素,但它总是显示最后一个,所以我猜测以前的条目被覆盖了?

Here's the piece of the code: 这是代码片段:

for(var i=0;i<shows.length;i++){  
    if(fgenCTRL.value===films[i].genre)
    {

     filmDiv.innerHTML=shows[i].title+"<br>";

    }

}

It should display 2 elements as an output but it only displays one. 它应该显示2个元素作为输出,但它只显示一个。 Please excuse my poor use of English and Coding Skills. 请原谅我对英语和编码技巧的不良使用。

for(var i=0;i<shows.length;i++){  
    if(fgenCTRL.value===films[i].genre)
    {

     filmDiv.innerHTML+=shows[i].title+"<br>";

    }

}

you have to add the new value , not set each time. 你必须添加新值,而不是每次都设置。

Well yeah in your code, you just replace your value, try using this code. 好吧,在您的代码中,您只需替换您的值,尝试使用此代码。

for(var i=0;i<shows.length;i++){  
    if(fgenCTRL.value===films[i].genre)
    {

     filmDiv.innerHTML=filmDiv.innerHTML+shows[i].title+"<br>";

    }

}

Yes, setting the .innerHTML properties overwrites any previous value for that property. 是的,设置.innerHTML属性会覆盖该属性的任何先前值。

If you want to append your info to it, you have to code it to append by adding to the previous HTML, not overwriting the previous value (note the use of += here). 如果你想将你的信息附加到它,你必须通过添加到以前的HTML来编码它以附加,而不是覆盖以前的值(注意使用+= here)。

for(var i=0;i<shows.length;i++){  
    if(fgenCTRL.value===films[i].genre) {
      filmDiv.innerHTML += shows[i].title+"<br>";
    }
}

Though generally, it is better NOT to use .innerHTML this way because you are repetitively converting everything from DOM elements to HTML, adding something to the HTML, then parsing everything back to DOM elements again, creating all new DOM elements and getting rid of all the previous DOM elements. 虽然一般情况下,最好不要以这种方式使用.innerHTML因为你重复地将所有内容从DOM元素转换为HTML,向HTML添加内容,然后再将所有内容解析回DOM元素,创建所有新的DOM元素并删除所有内容以前的DOM元素。

It is better to just use .appendChild() to append new DOM elements, leaving your previous DOM elements in place rather than recreating everything every time like this: 最好只使用.appendChild()来追加新的DOM元素,将以前的DOM元素保留在原位,而不是每次都重新创建所有内容:

for(var i=0;i<shows.length;i++){  
    if(fgenCTRL.value===films[i].genre) {
      filmDiv.appendChild(document.createTextNode(shows[i].title));
      fileDiv.appendChild(document.createElement("br"));
    }
}

innerHTML is a DOM property that contain a string. innerHTML是一个包含字符串的DOM属性。 It is not a function that add something like appendChild . 它不是添加像appendChild这样的函数。

You need to concatenate the string if you don't want to override it 如果您不想覆盖它,则需要连接该字符串

filmDiv.innerHTML += shows[i].title+"<br>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM