您每次都覆盖其他对象,您需要使用数组来保存所有对象:
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {
'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()
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
您可能还需要考虑使用对象而不是数组,并使用产品名称作为关键.这将防止在localStorage中显示的重复条目.