gpt4 book ai didi

jquery - 动态追加元素到 jQuery Mobile ListView

转载 作者:行者123 更新时间:2023-12-03 22:35:21 26 4
gpt4 key购买 nike

我想动态地将通过 JSOn 格式的 url 接收到的数据附加到我的 ListView 中。但我不知道它是如何工作的。

移动网站按以下格式检索对象:

[
{"id":1, "start":"2011-10-29T13:15:00.000+10:00", "end":"2011-10-29T14:15:00.000+10:00", "title":"Meeting"}
]

在 .html 中,我有一个 ListView 和一个函数,我尝试在其中附加接收到的数据。我只展示了 body 。

<body>
<div>
<ul id="listview">
<script>$.getJSON("url",
function(data){
$.each(data, function(i,data){
i.title.appendTo("#listview");
});});</script>
</ul>
</div>
</body>

可能很简单,但我是网络编程新手,我不知道应该如何附加检索到的数据。

谁能帮帮我吗?

最佳答案

//make AJAX call to url
$.getJSON("url", function(data){

//declare a variable with which to build our output (it's best to buffer output and only do one append at the end since DOM manipulation is CPU expensive)
var output = '';

//iterate through the data (we could also get rid of the jQuery here by using `for (key in data) {
$.each(data, function(index, value){

//add each value to the output buffer (we also have access to the other properties of this object: id, start, and end)
output += '<li>' + value.title + '</li>';
});

//now append the buffered output to the listview and either refresh the listview or create it (meaning have jQuery Mobile style the list)
$('#listview').append(output).listview('refresh');//or if the listview has yet to be initialized, use `.trigger('create');` instead of `.listview('refresh');`
});

这是上述解决方案的 jsfiddle(还有一个使用 for(){} 而不是 $.each() 的示例):http://jsfiddle.net/VqULm/

关于jquery - 动态追加元素到 jQuery Mobile ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com