javascript数组到html <li>

时间:2016-04-06 10:51:09

标签: javascript arrays json arraylist

我从json获得了一个数组,我需要将每个项目放在<li>上我的html

类似的东西:

names : {john, paul, ringo,george}
into <li>john</li>..

我的代码:

<div id="demo"></div>

脚本:

function onLocationsReceived(data) {
  console.log("recievd");

  for (var i = 0; i < data[0].Sensors.length; i++) {
    var sensorNames = data[0].Sensors[i].Name;
    document.getElementById("demo").innerHTML = sensorNames;
    console.log(sensorNames);
  }
}

在concole.log上打印得很好..

document.getElementById("demo").innerHTML = '<li>' + sensorNames '</li>

类似的东西???

3 个答案:

答案 0 :(得分:1)

您可以使用以下语法

Option Explicit

Sub CountInvestorsByTwitterName2()
    Dim row_total As Long
    Dim Unqiue_Twitter_Names As Range
    Dim found As Range

    Dim sht As Worksheet
    Dim r As Range, shtRng As Range

    With Application
        .Calculation = xlCalculationManual: .ScreenUpdating = False: .DisplayAlerts = False
    End With

    With Sheets("UNIQUE_DATA")
        .Columns("B:XFD").EntireColumn.Delete
        Set Unqiue_Twitter_Names = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeConstants, xlTextValues)
    End With

    For Each sht In Sheets
        With sht
            If .Name <> "UNIQUE_DATA" Then
                Set shtRng = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row).SpecialCells(xlCellTypeConstants, xlTextValues)
                For Each r In shtRng
                    Set found = Unqiue_Twitter_Names.Find(What:=r.Value, LookIn:=xlValues, LookAt:=xlWhole)
                    If Not found Is Nothing Then
                        With found
                            .Offset(0, 1).Value = CDbl(.Offset(0, 1).Value) + 1
                            .End(xlToRight).Offset(0, 1).Value = sht.Name
                        End With
                    End If
                Next
            End If
        End With
    Next

    With Application
        .Calculation = xlCalculationAutomatic: .ScreenUpdating = True: .DisplayAlerts = True
    End With
End Sub

答案 1 :(得分:1)

使用以下内容

function onLocationsReceived(data){
var html="";
  for (var i = 0; i < data[0].Sensors.length; i++) {
   var sensorNames = data[0].Sensors[i].Name;
    html+="<li>"+sensorNames+"</li>";
    console.log(sensorNames);
}
document.getElementById("demo").innerHTML=html;
}

答案 2 :(得分:0)

您应该使用li将迭代的sensorNames缓存到var中,然后替换innerHTML:

var content = "",
    sensorNames;

for (var i = 0; i < data[0].Sensors.length; i++) {
    sensorNames = data[0].Sensors[i].Name;
    content += "<li>" + sensorNames + "</li>";
}

document.getElementById("demo").innerHTML = content;