我有一个动态列表视图,我想做的是将当前单击的列表存储到本地存储并检索它。我有以下代码,它似乎可以工作,但我不确定是否有人可以提醒我它是否正确,因为我需要对数据做更多的事情。
列表视图的 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);
});