<!doctype html> <html> <head> <meta charset="utf-8"> <title>localStorage使用示例</title> </head> <body> <script type="text/javascript"> if (window.localStorage) { var fruit = new Array('apple', 'banana', 'orange'); // 存储数据(localStorage存储的数据是永久有效,只要不手动删除就会一直存在) var json = JSON.stringify(fruit); // 数组转JSON字符串 localStorage.setItem('fruit', json); // 获取数据 var string = localStorage.getItem('fruit'); var array = JSON.parse(string); console.log(array); // 删除数据 localStorage.removeItem('fruit'); // 清空所有数据 localStorage.clear(); } else { alert('浏览器不支持localStorage本地存储'); } </script> </body> </html>
Copyright © 2024 码农人生. All Rights Reserved