我正在尝试将已从外部json文件填充的localstorage数据保存为"收藏夹"。 具体地,
我在Jquery mobile 1.4.5中有一个listview,当点击每个项目时会创建一个动态页面。 现在我希望该页面能够保存为最爱以供以后使用。
到目前为止我的代码:
HTML:
<div data-role="page" id="ccc">
<div data-role="header">
<h1>My page</h1>
</div>
<div role="main" class="ui-content">
<ul id="mylist" data-role="listview" data-inset="true">
</ul>
</div>
JAVASCRIPT:
$(document).on("pagebeforeshow", "#ccc", function () {
$.getJSON('https://api.myjson.com/bins/4khhg', function(data) {
var output="";
var newpage="";
for (var i in data.mydata) {
output+="<li>" +
"<a href=\"#page" + data.mydata[i].myId + "\">" +
"<h3>" + data.mydata[i].thename + "</h3>" +
"<p>" + data.mydata[i].desciption + "</p>" + "</a>" +
"</li>";
//see if page already exists in DOM
if ($("#page" + data.mydata[i].myId).length == 0) {
newpage+="<div data-role=\"page\" id=\"page" + data.mydata[i].myId + "\">";
newpage+="<div data-role=\"header\">"+
"<a data-role=\"button\" data-rel=\"back\" data-icon=\"back\">Back</a>" +"<h3>" +
data.mydata[i].thename + "</h3>" +
"<a data-role=\"button\" data-icon=\"star\" class=\"saveit\" data-saveid=\"" + data.mydata[i].myId +
"\">Save it button</a>" + "</div>";
newpage+="<div role=\"main\" class=\"ui-content\">" +
data.mydata[i].desciption + "</div> </div>";
}
}
output+="";
newpage += "";
$('body').append(newpage);
$("#mylist").html(output).listview('refresh');
////////////////// start save function ///////////////
$(".saveit").click(function (){
var saveditems = [];
saveditems[0] = "<h3> " + data.mydata[i].thename + "</h3> " + data.mydata[i].desciption ;
localStorage["saveditems"] = JSON.stringify(saveditems);
var storeditemss = JSON.parse(localStorage["saveditems"]);
alert(saveditems);
});
////////////////// end save function ///////////////
});
});
我的主要问题是:
任何帮助或提示?
谢谢你!