仅在第一次使用localStorage加载网站时显示DIV [英] Showing a DIV only the first time a website is loaded with localStorage

查看:198
本文介绍了仅在第一次使用localStorage加载网站时显示DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使这项工作,但我有一些问题..就像标题所说,我希望只在第一次加载网站时才显示DIV。我知道如何处理PHP和Cookies,但我想用localStorage函数。

I am trying to make this work but I have some problems.. Like the title says, I want a DIV to be showed only the first time a website is loaded. I know how to do with PHP and Cookies, but I want it with the localStorage function.

这是我的代码:

<div id="loading_bg">
  ...
</div>


$(document).ready(function() {
    localStorage.setItem('wasVisited', 1);
    if (localStorage.wasVisidet !== undefined ) {
            $("#loading_bg").css('display','none');
        } else {
            $("#loading_bg").delay(5000).fadeOut(500);
        }
});


推荐答案

您在if条件之前设置localStorage标志,所以标志 wasVisited 永远不会被定义。

You are setting the localStorage flag before the if condition, so the flag wasVisited will never be undefined.

你需要在else块中设置标志,如下所示

You need to set the flag inside the else block as shown below

$(document).ready(function () {
    if (localStorage.getItem('wasVisited') !== undefined) {
        $("#loading_bg").hide();
    } else {
        localStorage.setItem('wasVisited', 1);
        $("#loading_bg").delay(5000).fadeOut(500);
    }
});

这篇关于仅在第一次使用localStorage加载网站时显示DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆