1

我希望 jquery 代码在 div 标签中动态创建以下 Ul 名称和时间不同。

我有 json 格式的数据库中的名称和日期,我想使用 Jquery 从数据中创建菜单。

    <div data-role="content">
                <ul data-role="listview" data-divider-theme="b" data-inset="false">
                    <li data-role="list-divider" role="heading"></li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Verinoff,James <span class="patient-list_txt2-red">04/13/2011 12:12</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Parker,Tollin <span class="patient-list_txt2-yellow">04/13/2011 12:10</span> </a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Michael,John <span class="patient-list_txt2-green">04/13/2011 12:04</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Jonah <span class="patient-list_txt2-yellow">04/13/2011 12:03</span></a> </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Jaffee,Dillon <span class="patient-list_txt2-red">04/13/2011 12:02</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Denny Stephen <span class="patient-list_txt2-blue">04/13/2011 12:01</span></a>
                    </li>
                </ul>
            </div>
4

2 回答 2

2
$.each(json, function() {
   $("div[data-role=content] ul")
     .append(
        $("<li>").data('theme', 'c')
           .append($("<a>").data('transition', 'slide')
              .click(navigation_message_details)
              .text(this.name)
              .append($("<span>").addClass('patient-list_txt2-' + this.class)
                 .text(this.date)
              )
           )
        )
     ;
});

我不知道类的颜色来自哪里,所以我只是假设它是json.

于 2012-12-14T14:59:16.797 回答
0

试试这个代码,例如这可以是你的 json (你从 ajax 服务器调用中获取):

var yourJson = { "data" : [{"name" : "Paolo", "dateTime" : "04/13/2011 12:01"}] };

然后你可以迭代它:

$.each(yourJson.data, function(i)
{
    var li = $('<li/>')
    .addClass('yourCssClass')
    .attr('data-theme', 'c')
    .appendTo(list);

    var aaa = $('<a/>')
    .html(yourJson.data[i].name + " <span class='patient-list_txt2-blue'>" + yourJson.data[i].dateTime + "</span>")
    .attr('data-transition', 'slide')
    .appendTo(li);
});

这是一个小提琴:http: //jsfiddle.net/pbBav/

于 2012-12-14T15:13:24.737 回答