前端学习(1043):回车把数据存储到本地存储里面
生活随笔
收集整理的這篇文章主要介紹了
前端学习(1043):回车把数据存储到本地存储里面
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>todoList待辦事項(xiàng)</title><script src="./js/jquery.min.js"></script>
</head><body><header><section><label for="title">ToDoList</label><input type="text" id="title" placeholder="添加ToDo"></section></header><section><h2>正在進(jìn)行<span id="rodocount"></span></h2><ol id="todolist" class="demo-box"></ol><h2>已經(jīng)完成<span id="rodocount"></span></h2><ul id="donelist"></ul></section><footer>Copyright ©2014 todolist.cn</footer><script>var todolist = [{title: '我今天吃了八個(gè)饅頭',done: false}, {title: '我今天學(xué)習(xí)jq',done: false}, ];localStorage.setItem("todo", JSON.stringify(todolist));//本地的數(shù)組轉(zhuǎn)換為json.stringfy();var data = localStorage.getItem("todo");console.log(typeof data);//獲取本地存儲(chǔ)數(shù)據(jù)需要把字符串?dāng)?shù)據(jù)轉(zhuǎn)換為對(duì)象形式j(luò)son.parsedata = JSON.parse(data);console.log(data);$(function() {//按下回車 把完整數(shù)據(jù)存儲(chǔ)到本地存儲(chǔ)里面$("#title").on("keydown", function(event) {if (event.keyCode === 13) {//先讀取本地存儲(chǔ)原來的數(shù)據(jù)var local = getDate();console.log(local);}})//讀取本地存儲(chǔ)的數(shù)據(jù)function getDate() {var data = localStorage.getItem("todolist");if (data !== null) {//本地存儲(chǔ)里面的數(shù)據(jù)是字符串格式的return JSON.parse(data);} else {//返回一個(gè)數(shù)組return [];}}});</script>
</body></html>
運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的前端学习(1043):回车把数据存储到本地存储里面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle数据库下载
- 下一篇: 2017年html5行业报告,云适配发布