我有一个动态列表视图,我想要做的是存储当前被点击到本地存储的列表并检索它。我有以下代码,它似乎工作,但我不确定,如果它是正确的,有人可以给我一个抬头,因为我需要做更多的数据。
列表视图的html:
<ul data-role="listview" id="newsfeedposts" data-theme="a" data-overlay-theme="a" data-inline="true"data-inset="true"></ul>
每个列表项的标记:
markup += '<li><a href="#fbfullpostviewpage" class="item" data-rel="dialog" data-transition="pop" data-overlay-theme="e" data-inline="true"><img src="' + thumb_url + '">' +'<h4>' + name + '</h4><p>' +'posted this photo....</p><p>'+likes+'<img src="images/facebook-like-16.png"></p></a></li>';
$('#newsfeedposts').append(markup);
addPostToLocalStorage(posts);
$('#newsfeedposts').listview("refresh");
将项目添加到存储并检索它的方法;
function addPostToLocalStorage(facebookPost){
$(".item").on('click', function (){
var i = $('.item').index(this);
console.log(i);
localStorage['results'] = JSON.stringify(facebookPost[i]);
storedItem = (localStorage['results']) ;
console.log(storedItem);
alert("this post was added to local storage");
//retrieve list item
var retrievedItem = localStorage.getItem(['results']);
console.log( 'retrieved: ' + retrievedItem);
});