简体   繁体   English

如何在本地存储中存储 JavaScript 变量?

[英]How can I store a JavaScript variable in local storage?

So I have a JavaScript variable called addNumber which just adds a number by 1 to a div any time I click a button.所以我有一个名为addNumber的 JavaScript 变量,只要我点击一个按钮,它就会将一个数字加 1 到一个 div。 But I'm using it on a website which has multiple pages, and every time I change pages, it resets the counter.但是我在一个有多个页面的网站上使用它,每次我更改页面时,它都会重置计数器。 I've used console.log() to check the variable multiple times, and it always resets back to zero any time I leave the page, even when I keep the tab open.我已经使用console.log()多次检查变量,并且在我离开页面时它总是重置为零,即使我保持标签打开也是如此。 This counter is kept at the top of the page for all to see, and I want it kept there in a navbar, kind of like the shopping cart on Amazon.这个柜台保留在页面顶部供所有人查看,我希望它保留在导航栏中,有点像亚马逊上的购物车。 Is there a way I can do something like that using only vanilla JavaScript?有没有办法只使用香草 JavaScript 来做类似的事情?

Thank you.谢谢你。

You may find this link helpfull.您可能会发现此链接很有帮助。 https://www.w3schools.com/jsref/prop_win_localstorage.asp https://www.w3schools.com/jsref/prop_win_localstorage.asp

Quick answer you store key value pairs using a string tag to identify them:快速回答您使用字符串标签存储键值对以识别它们:

localStorage.setItem("lastname", "Smith");
localStorage.getItem("lastname"); //this returns the string "Smith"

Edit: i do not recommend you doing this in a production enviroment.编辑:我不建议你在生产环境中这样做。 You should stick to the framework of the project.你应该坚持项目的框架。 If the project has no framework or is just for school or learning purposes then i guess is a viable option.如果该项目没有框架或仅用于学校或学习目的,那么我想这是一个可行的选择。

You can store a variable in localStorage to access it somewhere else through您可以将变量存储在 localStorage 中,以便通过

Try using:尝试使用:

function onClickevent(){
    addNumber = addNumber +1 
    localStorage.setItem('counter', addNumber);
}

Then get it through然后通过

//Just assign it to your div 
const getIt = localStorage.getItem('counter')

Here is some documentation on LocalStorage and how to use it:) https://www.w3schools.com/jsref/met_storage_getitem.asp以下是有关 LocalStorage 的一些文档以及如何使用它:) https://www.w3schools.com/jsref/met_storage_getitem.asp

https://blog.logrocket.com/localstorage-javascript-complete-guide/#:~:text=To%20get%20items%20from%20localStorage%2C%20use%20the%20getItem()%20method,in%20the%20browser's%20localStorage%20object. https://blog.logrocket.com/localstorage-javascript-complete-guide/#:~:text=To%20get%20items%20from%20localStorage%2C%20use%20the%20getItem()%20method,in%20the% 20浏览器的%20localStorage%20对象。

The solution you can try is:您可以尝试的解决方案是:

In the.js file at the end of all lines, type在所有行末尾的 .js 文件中,键入

window.localstorage.setItem("addNumber",addNumber); window.localstorage.setItem("addNumber",addNumber);

The addNumber will be stored with an ID of addNumber. addNumber 将与 addNumber 的 ID 一起存储。

To later retrieve, use:要稍后检索,请使用:

window.localstorage.getItem("addNumber"); window.localstorage.getItem("addNumber");

You may assign the above to a variable like您可以将以上内容分配给一个变量,例如

var number =....变数 =....

Then console the number.然后控制台号码。

I hope this helps.我希望这有帮助。

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

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