我有幾個localStorage
關鍵字,例如Cart,Viewed和Trash。LocalStorage,通過匹配的ID進行循環,從DOM中刪除
的問題是雙重的:
1)如何可能我環路雖然該項目的ID中的localStorage以最高效的方式,如果一個ID已經存在,類或數據屬性添加到匹配的DOM元素。
2)從3本地存儲Keys
(垃圾箱,購物車,查看)創建最後20個項目的單個列表的最佳方式是什麼?從性能角度來看,是否最好在我的問題的pt1中使用相同的循環函數來做到這一點(也就是測試一個ID是否存在並添加一個類,如果爲true)?
這是我有:
HTML
<article data-id="548" data-date="Mon Dec 19 09:12:57"> <h1><a href="#">Title</a></h1> // section // footer <a href="#" data-context="trash">trash</a> </article>
jQuery的
$tools.on("click", "a", function(e){
var storeId = $(this).attr('data-context');
var selector = $(this).closest('article');
var dataId = selector.attr('data-id');
var pubDate = selector.attr('data-date');
var postUrl = selector.find('a', 'h1').attr('href');
var postTitle = selector.find('h1').text();
var $removable = $container.find(selector);
setLocalStorage(storeId, dataId, postUrl, postTitle, pubDate);
if (storeId == "Trash") {
$container.isotope('remove', $removable);
};
e.preventDefault();
});
setter函數。
變量通過諸如data-context="Trash"
(關鍵字)和data-id="001"
(id)並存儲在相關密鑰中。
function setLocalStorage(itemKey, dataId, postUrl, postTitle, postPub) {
var ItemKey = itemKey;
var timeStamp = new Date();
var json = JSON.parse(localStorage.getItem(ItemKey));
if(json == null)
json = [];
json.push({id:dataId,title:postTitle,url:postUrl,postPub:postPub,dataTimeStamp:timeStamp});
// save the result array
sessionStorage.setItem(ItemKey, JSON.stringify(json))
// Notify user item added to Cart
updateNotifications(ItemKey, json);
}
的localStorage的結束是這樣的:Trash [{"id":"418","title":"\n\t\t\t\t\t\t\t\t\tPost Title....//..."}]
這裏任何幫助將是非常讚賞。
新年快樂!
乾杯 本