仅在第一次使用localStorage加载网站时显示DIV

编程入门 行业动态 更新时间:2024-10-27 08:30:32
本文介绍了仅在第一次使用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

本文发布于:2023-10-12 21:40:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   网站   localStorage   DIV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!