javascript - 检查localStorage中是否有值,如果没有显示替代消息(Javascript)
问题描述
我仍然是新手开发人员,非常感谢一些帮助。当用户在测验中得分 10/10 时,从另一个函数中调用下面的函数 on()。用户名最初是从不同页面上的用户名登录创建的,并且该值成功存储在 localStorage 中。该部分工作正常,如果我省略了第一个“if”语句行和整个“else if”语句,我可以使用以下语句的第一部分调用该值(在 on() 函数中)。但是,我试图在用户不输入用户名的情况下迎合时间,因为当调用 on() 函数的第一部分时,它会显示以下内容:“恭喜{null}!你获得了完美的 10 分”。我希望它在下面的“else if”部分中显示备用消息。
function on() {
if (localStorage.getItem("users") == true) {
document.getElementById("overlay").style.display = "block";
document.getElementById("overlay").innerText = "Congratulations " +
window.localStorage.getItem('users') + "! You scored the perfect 10!";
} else
if (localStorage.getItem("users") === null) {
document.getElementById("overlay").style.display = "block";
document.getElementById("overlay").innerText = "Congratulations! You scored the perfect 10!";
}
解决方案
推荐阅读
- nuxeo - 覆盖 Nuxeo 中的默认图片转换
- nginx - Nginx - “systemctl status nginx”上的错误
- primefaces - 如何使用 Java 更改 Primefaces 7.0 ChartJS Linechart 的 Y 轴?
- php - 如何在 mysql json Array 数据中编写 SELECT 查询
- reactjs - 使用 react-picky 选择菜单时出现问题
- laravel - Laravel 控制器/商店
- amazon-web-services - 如何使用 Cloudformation 将代码从 aws s3 存储桶安全地提取到 Codecommit 存储库?
- python - 为什么我的元音删除功能不起作用?(Python 2.7)
- jhipster - Jhipster 重置电子邮件
- arrays - 我们可以降低将数组中的每个用户相互比较的复杂性吗?