简体   繁体   English

如何将循环的每个迭代存储为innerHTML

[英]How to store each iteration of a loop as innerHTML

As a JS novice I am trying to store all values of an object inside separate divs. 作为JS新手,我正在尝试将对象的所有值存储在单独的div中。 (per div the name of the chord and an image). (每格的和弦名称和图像)。

I get all values logged to the console, but when I store it inside a div I only get the final outcome of the loop, being the last value. 我将所有值记录到控制台,但是当我将其存储在div中时,我只会得到循环的最终结果,即最后一个值。 In other words: I cant seem to find out how to store the values inside a div for each time the loop runs. 换句话说:我似乎无法找出每次循环运行时如何将值存储在div中。

Thank you in forehand! 正手谢谢你!

here the stuff: 这里的东西:

 var output = document.getElementById('output'); // chords & tabs images var chords = { "C": { "Chord" : "C", "imgSrc" : "http://www.guitar-chords.org.uk/chord-images/c-major-1.gif" }, "Cmaj9": { "Chord" : "Cmaj9", "imgSrc" : "http://www.totalguitarandbass.com/system/diagrams/495/original/CMaj9.png?1472445766" } }; // looping through for(var key in chords) { var value = chords[key]; // log console.log(value); // my silly attempt output.innerHTML = "<div class=\\"chord\\">" + "<h1>" + value.Chord + "</h1>" + "<img src=\\"" + value.imgSrc + "\\"/>"; } 
 div { box-sizing:border-box; display:block; } .chord { display:block; float:left; width:250px; height:auto; border:1px solid tomato; } .chord h1 { font-size:2.4em; text-align:center; padding:5px; } .chord img { display:block; margin-top:25px; width:200px; height:auto; padding:25px; } 
 <div id="output"></div> 

Using = overwrites any prior changes to the property. 使用=会覆盖对该属性的所有先前更改。 You should use the += operator to append the next element to the innerHTML: 您应该使用+ =运算符将下一个元素附加到innerHTML:

 var output = document.getElementById('output'); // chords & tabs images var chords = { "C": { "Chord" : "C", "imgSrc" : "http://www.guitar-chords.org.uk/chord-images/c-major-1.gif" }, "Cmaj9": { "Chord" : "Cmaj9", "imgSrc" : "http://www.totalguitarandbass.com/system/diagrams/495/original/CMaj9.png?1472445766" } }; // looping through for(var key in chords) { var value = chords[key]; // log console.log(value); // vvvv Right here vvvvv output.innerHTML += "<div class=\\"chord\\">" + "<h1>" + value.Chord + "</h1>" + "<img src=\\"" + value.imgSrc + "\\"/>"; } 
 div { box-sizing:border-box; display:block; } .chord { display:block; float:left; width:250px; height:auto; border:1px solid tomato; } .chord h1 { font-size:2.4em; text-align:center; padding:5px; } .chord img { display:block; margin-top:25px; width:200px; height:auto; padding:25px; } 
 <div id="output"></div> 

Here is a fiddle: https://jsfiddle.net/treyeckels/jszzg1oa/ 这是一个小提琴: https : //jsfiddle.net/treyeckels/jszzg1oa/

The content of the div is always being replaced by your last line in your for loop, which is why the last item in your content array is what's always only displayed. div的内容总是被for循环中的最后一行替换,这就是为什么内容数组中的最后一项总是只显示的原因。

innerHTML is a property of the output node that you are retrieving using getElementById , so you are writing over the innerHTML of the node every loop. innerHTML是您使用getElementById检索的output节点的属性,因此您需要在每个循环中覆盖该节点的innerHTML。

Instead what you want to do is create new DOM nodes and append them to the last child in the output div or if you want to continue to use innerHTML , concatenate all of the contents together as a string and then set output innerHTML to the concatenated string. 相反,您想要做的是创建新的DOM节点,并将其附加到output div中的最后一个子节点,或者如果您想继续使用innerHTML ,将所有内容串联为一个字符串,然后将output innerHTML设置为串联的字符串。

Something like (in es6): 类似于(在es6中):

const chords = [
    {
        "Chord" : "C",
        "imgSrc" : "http://www.guitar-chords.org.uk/chord-images/c-major-1.gif"
    },
    {
            "Chord" : "Cmaj9",
        "imgSrc" : "http://www.totalguitarandbass.com/system/diagrams/495/original/CMaj9.png?1472445766"
    }
];

const getHTML = (chord, imgSrc) => {
    return(
    `<div class="chord">
        <h1>${chord}</h1>
      <img src="${imgSrc}" />
    </div>`
  )
};

const completeHTML = chords.reduce((prev, next) => {
        return prev + getHTML(next.Chord, next.imgSrc);
}, '');

const output = document.getElementById('output');
output.innerHTML = completeHTML; 

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

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