var image2 =‘http://im.gifbt.com/images/star1_phonehover.png’; var image1 =‘http://im.gifbt.com/images/star1_phone.png’; var toggle =1;
function favaorite(sess_id,name,city,country,event_url,pointer){ var eventData; //is anything in localstorage? if(localStorage.getItem(‘eventData’)===null){ eventData =[]; }else{ // Parse the serialized data back into an array of objects eventData = JSON.parse(localStorage.getItem(‘eventData’)); console.log(eventData); } var details={}; details.sess_id= sess_id; details.name= name; details.city= city; details.country= country; details.event_url= event_url;
// Push the new data (whether it be an object or anything else) onto the array eventData.push(details);
}); toggle =1; } // Alert the array value // alert(eventData); // Should be something like [Object array] // Re-serialize the array back into a string and store it in localStorage var jsondata=localStorage.setItem(‘eventData’, JSON.stringify(eventData)); console.log(jsondata); }
小提琴
您可以尝试使用纯js吗?它可能会以某种方式强制缓存。或者强制刷新:复制一个节点中的目标,然后用新的 bg 将该节点读取到 dom
// declare an empty object (not array): var eventData ={}; // check localStorage first after the page is ready (not on click): if(localStorage.getItem(‘eventData’)!==null){ // if there’s any faved link in the localstorage, highlight it: $.each(eventData = JSON.parse(localStorage.getItem(‘eventData’)),function(id){ $(‘#’+id).addClass(‘faved’); }); }
// instead of ‘onclick’ attribute, use jQuery .click() event handler: $(‘.favourate_dextop’).click(function(e){ // prevent default link click (instead of using"javascript:void(0);" in a href attribute): e.preventDefault(); // check if the link"id" attribute (this.id) exists in the object: if(eventData[this.id]!==undefined){ // if so, delete it: delete eventData[this.id]; }else{ // if not, add it: // the .data() method gets all ‘data’ attributes of the element (as an objct) eventData[this.id]= $(this).data(); } // toggle ‘.faved’ class: $(this).toggleClass(‘faved’); // update localstorage: localStorage.setItem(‘eventData’, JSON.stringify(eventData)); }); });