localstorage学习笔记

console.log(localStorage);

//写入数据

localStorage.setItem('name','lily');

//读出数据

console.log(localStorage.getItem('name'));//lily

//缓存数据的长度

console.log(localStorage.length);// 1

//读取存储在localStorage的所有数据

console.log(localStorage.valueOf());// Storage {name: "lily", length: 1}

// 读取第一条数据的变量名

console.log(localStorage.key(0));//  name

//删除某个变量

localStorage.removeItem("name");

console.log(localStorage.getItem('name'));//  null

//检查localStorage里是否保存某个变量

localStorage.hasOwnProperty('name');// true

console.log(localStorage.hasOwnProperty('name'));//  false

localStorage.hasOwnProperty('sex');// false

console.log(localStorage.hasOwnProperty('sex'))//false

//将数组转为本地字符串

vararr= ['aa','bb','cc'];// ["aa","bb","cc"]

localStorage.arr=arr;//["aa","bb","cc"]

//localStorage.setItem('arr',arr);

console.log(localStorage.getItem('arr'));

localStorage.arr.toLocaleString();// "aa,bb,cc"

console.log('=====>'+localStorage.arr.toLocaleString());

//将json存储到localstorage里

varstudents= {

xiaomin: {

name:"xiaoming",

grade:1

},

teemo: {

name:"teemo",

grade:3

}

};

students=JSON.stringify(students);//将JSON转为字符串存到变量里

console.log(students);

localStorage.setItem("students",students);//将变量存到localStorage里

varnewStudents=localStorage.getItem("students");

newStudents=JSON.parse(students);//转为JSON

console.log(newStudents);// 打印出原先对象

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • js简介 Js是一种基于事件和对象驱动的解释性、松散性的语言。 一切皆对象 javascript 布兰登艾奇 ...
    塔库纳玛哈哈阅读 1,245评论 0 2
  • H5中LocalStorage的使用 Document 注意的是,IE8-IE10中是需要服务器或者localho...
    WERH5知识分享阅读 2,045评论 0 2
  • 1.区域模块分解 : 按照空间区域,对复杂的案例效果,分解为几个较简单的模块部分。目的是:更方便的理解案例原理。 ...
    空谷悠阅读 559评论 0 1
  • JS使用技巧专题 1开发技巧 1.1函数使用 1.1.1函数声明方式 JS函数的写法总结 http://blog....
    Kevin_Junbaozi阅读 1,120评论 0 11
  • localStorage sessionStorage cookie: 只能存储4KB 浪费流量,每次会随着HTT...
    执著_7a69阅读 978评论 0 1