Displaying List Of JavaScript Objects As HTML List Items

前端 未结 5 1065
野趣味
野趣味 2021-01-23 17:58

When I attempt this, the HMTL page only displays the last object, instead of all the objects.

Here is my JavaScript file

var family = {
  aaron: {
    n         


        
5条回答
  •  猫巷女王i
    2021-01-23 18:41

    Remove elList because there is no point in having it...

    Then change

    document.getElementById('aaron-family').innerHTML = prop;
    

    To

    document.getElementById('aaron-family').innerHTML += prop;
    

    That way you are not constantly setting the innherHTML to prop. Also, you might find it better to change the function to the following in order to prevent from constantly getting the element.

    function list(family) {
      var elList = document.getElementById('aaron-family');
      for (var prop in family) {
        elList.innerHTML += prop;
      }
    }
    

    Hope this helps:)

提交回复
热议问题