我试图使购物车前端与localstorage,因为有一些模式窗口,我需要通过购物车项目信息那里。每次点击添加到购物车时,它都应该创建对象并将其添加到本地存储。我知道我需要把一切都放在阵列和推新对象数组,尝试多种解决方案后 - 不能得到它的工作向localstorage添加新对象
这就是我的(仅保存最后一个对象):
var itemContainer = $(el).parents('div.item-container');
var itemObject = {
'product-name': itemContainer.find('h2.product-name a').text(),
'product-image': itemContainer.find('div.product-image img').attr('src'),
'product-price': itemContainer.find('span.product-price').text()
};
localStorage.setItem('itemStored', JSON.stringify(itemObject));
ItemObject不itemObject?你的变量名称是错误的。 JavaScript区分大小写。 – tafoo85
我是小写的对象,你保存到'localStorage',而你创建的对象有一个大写字母I. – Jack
对不起,我只是在这里输入的不正确,现在修复了 – alexndm