简体   繁体   English

如何动态创建HTML中的列表?

[英]How to create list in HTML dynamically?

In my jQuery mobile app, I want to display the result from a web service in a list. 在我的jQuery移动应用程序中,我想在列表中显示Web服务的结果。 How do I create the list dynamically? 如何动态创建列表?

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 v is the value for that index. 其中a是列表内容的对象数组, ijQuery.each$.each )传递给回调函数的索引变量, v是该索引的值。


For reference: http://api.jquery.com/jQuery.each/ . 供参考: http : //api.jquery.com/jQuery.each/

var arr = ["list", "items", "here"];
$("div").append("<ul></ul>");
for(var i in arr) {
    var li = "<li>";
    $("ul").append(li.concat(arr[i]))
}

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

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