简体   繁体   English

如何在 Localstorage 中保存 state?

[英]How to save the state in Localstorage?

Good morning who is reading this message早上好谁正在阅读此消息

I'm still relatively new to programming and I have a question... I made a Navigation bar: body > div > nav > div > ul > li*10我对编程还是比较陌生,我有一个问题......我做了一个导航栏:body > div > nav > div > ul > li*10

I have the movement done in CSS我在 CSS 中完成了运动

 //Barra de Navegacion
  const list = document.querySelectorAll('.list');

  function activeLink(){
    list.forEach((item)=>
    item.classList.remove('active'));
    this.classList.add('active');
  
  if(list.classlist.contains('active')){
    localStorage.setItem('on','true');
  } else{
   localStorage.setItem('on','false');
  }
  };
  
  if (localStorage.getItem('on') === 'true'){
    list.classList.add('active');
            
  } else {
    list.classList.remove('active');
    
  };


  list.forEach((item)=>
  item.addEventListener('click',activeLink));

The problem is that when I refresh the page or go to another part of it, it does not save the changes.问题是当我刷新页面或 go 到它的另一部分时,它不会保存更改。 how could i do it with localstorage?我怎么能用本地存储呢?

I tried several ways and I can't find the way to do it我尝试了几种方法,但我找不到方法

Thank you very much in advance非常感谢您提前

在此处输入图像描述

VIDEO VIDEO VIDEO VIDEO VIDEO视频 视频 视频 视频 视频

I think this code is too far down.我认为这段代码太低了。

if (localStorage.getItem('on') === 'true'){
  list.classList.add('active');
} else {
    list.classList.remove('active');
};

You should set this set immediatly when the page loads, immediatly after您应该在页面加载时立即设置此设置,之后立即

const list = document.querySelectorAll('.list');

So the list has the correct class from the beginning.所以列表从一开始就有正确的类。

I doubt that using localStorage is the right way to achieve what you want to do.我怀疑使用 localStorage 是实现您想要做的事情的正确方法。 I suggest that each page should have its link highlighted when you open it.我建议每个页面在打开时都应突出显示其链接。 This can be achieved by adding classes manually to the link of each page, keeping in my mind it wouldn't be that many links in the navigation.这可以通过手动向每个页面的链接添加类来实现,记住导航中的链接不会那么多。

Not sure what you are trying to achieve with this part of the code, but the syntax you are using is correct.不确定您要使用这部分代码实现什么,但您使用的语法是正确的。 Here is some example of usage of the local storage API to repeat.下面是一些使用本地存储 API的示例来重复。

You can save key-value pair to the local storage using the syntax:您可以使用以下语法将键值对保存到本地存储:

localStorage.setItem('key', 'value');

You can retrieve the saved value with the following syntax:您可以使用以下语法检索保存的值:

localStorage.getItem('key');

If you still can't get the code to work correctly you can try to open your browser's developer tools and look at the storage.如果您仍然无法让代码正常工作,您可以尝试打开浏览器的开发人员工具并查看存储。 If your developer's tools are saying that there are no entries saved to the local storage you then know that the main problem is with saving the key-value pair.如果您的开发人员的工具说没有条目保存到本地存储,那么您就知道主要问题是保存键值对。

Here's how to open the developer tools in Chrome:以下是在 Chrome 中打开开发者工具的方法:

  • First press F12 on your keyboard (or Ctrl+Shift+J)首先按键盘上的 F12(或 Ctrl+Shift+J)
  • Follow the steps shown in the image below按照下图所示的步骤操作

在此处输入图像描述

If you still cannot get the code working you should check your browser settings to make sure there are no security settings enabled that are preventing your application from saving to the local storage.如果您仍然无法使代码正常工作,您应该检查您的浏览器设置,以确保没有启用阻止您的应用程序保存到本地存储的安全设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM