我必須從文本文件中檢索值並使用jquery mobile在listview中追加。
我能夠從文本文件檢索值,但我沒有得到預期的輸出。將文件中的值添加到jQuery手機中的列表視圖
我已經嘗試了很多次改變jQuery語法,但它沒有奏效。
我的文本文件有以下數據:
apple.jpe,apple,healthy,
banana.jpe,banana,good,
cherry.jpe,cherry,tasty,
cranberry.jpe,cranberry,sour,
grape.jpe,grape,wine,
orange.jpe,orange,citric
<body>
<div data-role="page" id="pageOne">
<div data-role="content" id="header">
This is my list view where I want to load content dynamically from file
<ul data-role="listview" data-inset="true" id="list">
</ul>
</div>
</div>
<script>
$(document).on("pageinit", "#pageOne", function(){
$.get("info.txt",function(data){
var items=data.split(',');
for(var i=0;i<items.length;){
$("#header ul").append('<li>'+'<a href="#">');
creating image element which is a thumb nail in list view
var img = $('<img />').attr({
'src': 'img/'+items[i],
'width': 50
}).appendTo('#list');
creating heading element to list view
$('#list').append('<h2>'+items[i+1]+'</h2>');
creating para element to list view
$('#list').append('<p>'+items[i+2]+'</p>'+'</a>'+'</li>');
i=i+3;
}
I have tried refresh for list view
$('#list').listview('refresh');
});
});
</script>
</body>
'追加()'和'刷新()'循環(如張貼在我的答案)的外面是確實快,在這裏看到:https://開頭learn.jquery.com/performance/append-outside-loop/ – deblocker