How to create list in HTML dynamically?

前端 未结 2 810
盖世英雄少女心
盖世英雄少女心 2021-02-01 10:16

In my jQuery mobile app, I want to display the result from a web service in a list. How do I create the list dynamically?

相关标签:
2条回答
  • 2021-02-01 10:29
    var arr = ["list", "items", "here"];
    $("div").append("<ul></ul>");
    for(var i in arr) {
        var li = "<li>";
        $("ul").append(li.concat(arr[i]))
    }
    
    0 讨论(0)
  • 2021-02-01 10:55

    Better yet,

    $.each(
        a ,
        function(i,v) {
            $("#target_id").append("<li>" + v + "</li>") ;
        }
    ) ;
    

    Where a is an Array of Objects for the list content, i is the index variable passed to the callback function by jQuery.each ($.each) and vis the value for that index.


    For reference: http://api.jquery.com/jQuery.each/ .

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