For loop through Array only shows last value

后端 未结 1 821
情深已故
情深已故 2020-12-18 09:25

I\'m trying to loop through an Array which then uses innerHTML to create a new

  • element for every entry in the array. Somehow my code is only showing the last value from
  • 1条回答
    •  半阙折子戏
      2020-12-18 09:52

      Change your for loop:

      for (i = 0; i <= (n-1); i++) {
          var list = names[i];
          var myList = document.getElementById("list");
          myList.innerHTML += "
    • "+ list + "
    • " + "
      "; }

      Use += instead of =. Other than that, your code looks fine.

      0 讨论(0)
    提交回复
    热议问题