Javascript本地存儲(chǔ)localStorage看這一篇就夠了
介紹
- 數(shù)據(jù)存儲(chǔ)在用戶瀏覽器中,其實(shí)是保存在硬盤(pán)中
- 頁(yè)面刷新不丟失數(shù)據(jù)
- sessionStorage和localStorage約 5M 左右
localStorage :
- 使用localStorage 可以將數(shù)據(jù)永久存儲(chǔ)在本地電腦中, 除非手動(dòng)刪除,否則關(guān)閉頁(yè)面也會(huì)存在。
- 可以多窗口(頁(yè)面)共享(同一瀏覽器可以共享)
- 以鍵值對(duì)的形式存儲(chǔ)使用
存儲(chǔ)數(shù)據(jù)到localStorage
語(yǔ)法
localStorage.setItem(key, value)
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 要存儲(chǔ)一個(gè)名字
localStorage.setItem('uname', 'tom')
</script>
</body>
</html>

打開(kāi)另外一個(gè)頁(yè)面,localStorage保存的信息照樣存在:

獲取localStorage的數(shù)據(jù)
語(yǔ)法
localStorage.getItem(key)
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 要存儲(chǔ)一個(gè)名字
localStorage.setItem('uname', 'tom')
// 獲取本地存儲(chǔ)
console.log(localStorage.getItem('uname'))
</script>
</body>
</html>

刪除localStorage的數(shù)據(jù)
語(yǔ)法
localStorage.removeItem(key)
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 要存儲(chǔ)一個(gè)名字
localStorage.setItem('uname', 'tom')
// 獲取本地存儲(chǔ)
// console.log(localStorage.getItem('uname'))
localStorage.removeItem('uname')
</script>
</body>
</html>
數(shù)據(jù)已經(jīng)刪除

修改localStorage的數(shù)據(jù)
修改localStorage的數(shù)據(jù)和localStorage新增數(shù)據(jù)的語(yǔ)法一樣。執(zhí)行localStorage.setItem(key, value)的時(shí)候,如果這個(gè)key已經(jīng)存在,就是修改;如果這個(gè)key不存在,就是新增。
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 要存儲(chǔ)一個(gè)名字
localStorage.setItem('uname', 'tom')
// 獲取本地存儲(chǔ)
// console.log(localStorage.getItem('uname'))
// localStorage.removeItem('uname')
// 修改數(shù)據(jù)
localStorage.setItem('uname', 'andy')
</script>
</body>
</html>

本地存儲(chǔ)只能存儲(chǔ)字符串
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 保存年齡
localStorage.setItem('age', 18)
console.log(localStorage.getItem('age'))
// 本地存儲(chǔ)的是字符串類(lèi)型
console.log(typeof localStorage.getItem('age'))
</script>
</body>
</html>
這個(gè)18是字符串類(lèi)型:

總結(jié)
到此這篇關(guān)于Javascript本地存儲(chǔ)localStorage的文章就介紹到這了,更多相關(guān)Javascript本地存儲(chǔ)localStorage內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript 本地存儲(chǔ)localStorage完全指南及應(yīng)用場(chǎng)景
- javascript中本地存儲(chǔ)localStorage,sessionStorage,cookie,indexDB的用法與使用場(chǎng)景匯總
- javascript中l(wèi)ocalStorage本地存儲(chǔ)(新增、刪除、修改)使用詳細(xì)教程
- JavaScript本地?cái)?shù)據(jù)存儲(chǔ)sessionStorage與localStorage使用詳解
- JavaScript中本地存儲(chǔ)(LocalStorage)和會(huì)話存儲(chǔ)(SessionStorage)的使用
- JS localStorage存儲(chǔ)對(duì)象,sessionStorage存儲(chǔ)數(shù)組對(duì)象操作示例
- JavaScript使用localStorage存儲(chǔ)數(shù)據(jù)
- JavaScript和jQuery存儲(chǔ)localStorage的具體實(shí)現(xiàn)方法詳解
相關(guān)文章
微信小程序?qū)崿F(xiàn)選項(xiàng)卡的簡(jiǎn)單實(shí)例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)選項(xiàng)卡的簡(jiǎn)單實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
前端大文件分片下載具體實(shí)現(xiàn)方法(看這一篇就夠了)
本文介紹了在瀏覽器中下載大文件的技術(shù)方案,包括分片下載、斷點(diǎn)續(xù)傳、進(jìn)度條顯示、取消及暫停下載和文件合并等功能,分片下載可以降低網(wǎng)絡(luò)傳輸中斷的風(fēng)險(xiǎn),并減少內(nèi)存占用,需要的朋友可以參考下2024-10-10
如何使Chrome控制臺(tái)支持多行js模式——意外發(fā)現(xiàn)
一直以來(lái),Chrome控制臺(tái)都缺少象IE調(diào)試臺(tái)那樣的多行執(zhí)行模式,今天意外發(fā)現(xiàn)Chrome其實(shí)也支持多行模式2013-06-06
深入理解JS中?Promise.all?和?Promise.allSettled?特性和區(qū)別應(yīng)用場(chǎng)景分析
在?JavaScript?中,Promise.all?和?Promise.allSettled?是處理多個(gè)?Promise?的常用方法,它們都可以用于并發(fā)執(zhí)行多個(gè)異步操作,但在行為、返回值和應(yīng)用場(chǎng)景上有顯著區(qū)別,本文將深入探討它們的特性、區(qū)別以及如何在實(shí)際開(kāi)發(fā)中選擇合適的方法,感興趣的朋友一起看看吧2025-05-05
JavaScript實(shí)現(xiàn)窮舉排列(permutation)算法謎題解答
這篇文章主要介紹了JavaScript實(shí)現(xiàn)窮舉排列(permutation)算法謎題解答,窮舉排列是指窮舉一個(gè)數(shù)組中各個(gè)元素的排列,需要的朋友可以參考下2014-12-12
使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

