我需要的
喜欢
id:1223,城市:“芝加哥”,
ID:12333,城市:“纽约”;
代码
function favaorite(sess_id,name,city,country,event_url,pointer)
{
/* store imageurl in localstorage */
var imageUrl='/images/star1_phonehover.png';
// Save data to the current local store//
// Put the object into storage
localStorage.setItem('id' ,JSON.stringify(sess_id));
// Put the object into storage
localStorage.setItem('name' ,JSON.stringify(name));
// Put the object into storage
localStorage.setItem('city',JSON.stringify(city));
// Put the object into storage
localStorage.setItem('country',JSON.stringify(country));
// Put the object into storage
localStorage.setItem('event_url',JSON.stringify(event_url));
// Put the object into storage
localStorage.setItem('imageUrl',JSON.stringify(imageUrl));
第2步。
/* fetch the data using from localstorage */
var id= [];
var name= [];
var city = [];
var country =[];
var event_url= [];
// Retrieve the object from storage
//var id, city, country,event_url;
var id = localStorage.getItem('id');
console.log(id);
id = JSON.parse(id);
var name = localStorage.getItem('name');
name = JSON.parse(name);
console.log(name);
var name = localStorage.getItem('name');
name = JSON.parse(name);
var city = localStorage.getItem('city');
city = JSON.parse(city);
console.log(city);
var country = localStorage.getItem('country');
country = JSON.parse(country);
console.log(country);
var event_url = localStorage.getItem('event_url');
event_url = JSON.parse(event_url);
///console.log(event_url);
var image_url = localStorage.getItem('imageUrl');
//event_url = JSON.parse(event_url);
alert(image_url);
console.log(image_url);
这是快照:
我也看过循环以在localstoarge中存储更多值。
for (var i = 0; i < localStorage.length; i++) {
console.log(localStorage.key(i))
};
这样尝试
您必须创建一个对象,并在每次单击时填写详细信息,将该对象推入数组,然后使用JSON.stringify()将数组存储在本地存储中。
并且在再次检索时,使用JSON.parse()解析该JSON。
function favaorite(sess_id,name,city,country,event_url,pointer){
var eventData = JSON.parse(localStorage.getItem('eventData '));
if(eventData ==null){
eventData=[];
}
Var details={};
details["sess_id"]=sess_id;
details["name"]=name;
details["city"]=city;
details["country"]=country;
details["event_url"]=event_url;
details["pointer"]=pointer;
eventData.push(details);
localStorage.setItem('eventData', JSON.stringify(eventData));
}
在检索将字符串解析为json时,您将获得这些单击事件详细信息的数组。
var EventDetails=JSON.parse(localStorage.getItem('eventData '));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句