带有收藏夹选项的多图像页面-如何使用localStorage来记住哪些图像受到了青睐
Multiple image page with favourite option - how to use localStorage to remember which images have been favourited
所以我有一个页面,上面有一个带有星形图标的图像,这样用户就可以喜欢了。每个图像都有这个。我希望用户能够点击,它会通过更改图像类来喜欢这些。使用localStorage意味着他们可以在未来的会话中返回,其中受支持的会话仍然受支持。到目前为止,我有:
if (window.localStorage) {
var didTheyFave = $('.icon-star-empty').each(function() {
(this).click(function() {
if($(this).hasClass('icon-star')) {
$( this ).addClass( 'icon-star-empty' );
$( this ).removeClass( 'icon-star' );
var fav = false;
} else {
$( this ).addClass( 'icon-star' );
$( this ).removeClass( 'icon-star-empty' );
var fav = true;
}
localStorage.setItem('didTheyFave', fav);
})
});
}
然后获取项目:
$(window).load(function() {
var favResult = localStorage.getItem('didTheyFave');
if ( favResult = true ) {
$('.icon-star-empty').addClass( 'icon-star' );
}
}); // I know this last bit is incorrect but testing things to see if they work!
$(window).load(function() {
function assignFavStar(){
$('.commonClassUsedforEachStar').each(function(index){
//get localStorage item for each
if ( favResult === true ) {//do this first
$('.commonClassUsedforEachStar').addClass( 'icon-star' );
} else {
$('.commonClassUsedforEachStar').addClass( 'icon-star-empty' );
}
});
}
}
如果你能做这样的事情,试试看,不是防弹的,但可以让你开始这样做。
if (window.localStorage) {
var didTheyFave = $('.icon-star-empty').click(function() {
if($(this).hasClass('icon-star')) {
$( this ).addClass( 'icon-star-empty' );
$( this ).removeClass( 'icon-star' );
var fav = false;
} else {
$( this ).addClass( 'icon-star' );
$( this ).removeClass( 'icon-star-empty' );
var fav = true;
}
localStorage.setItem('didTheyFave', fav);
});
}
localStorage.setItem('didTheyFave', fav);
这一行总是设置为true/false,并覆盖上一个值。您需要检查哪个设置为true,哪个设置为false。
您可以尝试在设置索引时使用索引,例如,当用户单击某个星形时,获取该星形的位置,获取该位置并将其添加到didTheyFave
,因此现在您保存在localStorage中的密钥看起来像didTheyFave1
或didTheyFave2
等等,您可以在检索时循环这些索引并设置true/false值。
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- LocalStorage phonegap摄像头图像
- 压缩捕获的图像并将其保存到localStorage
- 将图像加载到localStorage,并将图像src设置到该位置
- Javascript:图像到base64用于localStorage
- 如何将图像放入 javascript localstorage 条目中
- 使用 localStorage 存储和检索 PNG 时出现空白图像
- 带有收藏夹选项的多图像页面-如何使用localStorage来记住哪些图像受到了青睐
- 永久启用按钮并使用jQuery和LocalStorage更改其图像src