1

使用我的网站,人们可以搜索世界上的任何名字。用户可以将他们的特定名称添加到收藏列表。每个用户都可以在另一个页面中看到他们最喜欢的名称列表(用户在一个会话中添加的名称)。你能建议一个使用 jquery/ajax 的最佳方法吗?谢谢。

4

1 回答 1

1

您可以使用本地存储来保存数据。它就像这样简单:

localStorage.setItem('key', 'value');
localStorage.getItem('key'); // returns 'value'

您可以使用递增键保存名称:

localStorage.setItem('name-' + (localStorage.length + 1).toString(), 'favName');
// Names stored as 'name-0', 'name1', ...

然后,检索列表:

var names = new Array();

if (localStorage) {
    if (localStorage.length) {
       for (var i = 0; i < localStorage.length; i++) {
           names[i] = localStorage.getItem('name-' + i.toString());
       }
    } else {
       names[0] = 'You have no favorite names stored';
    }
}

有一些插件可以在不支持网络存储的浏览器上提供回退(即使在 ie6 上),例如totalStoragejstorage

于 2012-09-24T09:00:24.673 回答