适合seo优化的网站制作,jsp网站开发目的及意义,漳州网站开发去博大钱少a,久久建筑网怎么赚金币一篇关于HTML本地存储的文章 Window.localStorage 只读的localStorage 属性允许你访问一个Document 源#xff08;origin#xff09;的对象 Storage#xff1b;存储的数据将保存在浏览器会话中。 localStorage 类似 sessionStorage#xff0c;但其区别在于#xff1a;存储…一篇关于HTML本地存储的文章 Window.localStorage 只读的localStorage 属性允许你访问一个Document 源origin的对象 Storage存储的数据将保存在浏览器会话中。 localStorage 类似 sessionStorage但其区别在于存储在 localStorage 的数据可以长期保留而当页面会话结束——也就是说当页面被关闭时存储在 sessionStorage 的数据会被清除。
localStorage 可以被长期保留。sessionStorage页面关闭数据被清楚。localStorage 中的键值对总是以字符串的形式存储。 (需要注意和 js 对象相比键值对总是以字符串的形式存储意味着数值类型会自动转化为字符串类型). myStorage localStorage;//添加localStorage.setItem(myCat, Tom);//获取let cat localStorage.getItem(myCat);//删除
localStorage.removeItem(myCat);
// 移除所有
localStorage.clear();
如何获取localStorage中存储的所有值 直接使用localStorage即可获取的值是一个对象。在浏览器控制台中打印结果如下 storagelocalStorage;var length storage.lengthfor(var i0;ilength;i){var index storage.key(i);console.log(storage.getItem(index));}html
script//添加数据function add(){var text;textdocument.getElementById(text).value;indexlocalStorage.length1;localStorage.setItem(index,text);}//显示localStorage所有内容function showall(){storagelocalStorage;var length storage.lengthfor(var i0;ilength;i){var index storage.key(i);console.log(storage.getItem(index));}//将内容显示到html上方便交互。var showall document.getElementById(showall)showall.innerHTML JSON.stringify(storage);}function clearall(){//清空所有的itemlocalStorage.clear();}
/script
bodyp输入需要添加的数据/pinput idtext typetextbutton type onclickadd()点击添加数据/buttonbutton onclickshowall()显示所有记录/buttonbutton onclickclearall()清空所有记录/buttonp idshowall/p
/body
/html浏览器中查看Local Storage 输出的json对象是乱序排列的。