我希望jquery代码在div标签中创建以下UI,动态名称和时间不同。
我以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>
答案 0 :(得分: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
的一部分。
答案 1 :(得分: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/