我是靠谱客的博主 靓丽洋葱,这篇文章主要介绍分享html5中localStorage的实例教程,现在分享给大家,希望可以做个参考。

使用html5的storage来保存数据. 做了个小工具来用一下这个新特性。

需求说明: 有时发现有好的英语表达或者是陌生的单词,总是想记下来,但是过几天之后又不记得了,更别说运用了.

复制代码
中文解释:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>SO EASY!</title> <style type="text/css"> html,body { background-color: #262; color: #fff; font-family: helvetica, arial, sans-serif; margin: 0; padding: 0; font-size: 11pt; } </style> <script lang='text/javascript'> window.addEventListener("load", eventWindowLoaded, false); var entries = []; var curIndex = -1; function eventWindowLoaded() { loadEntries(1); showNext(); var dayselection = $("dayselection"); dayselection.addEventListener("change", daysSelectionChanged, false); } function daysSelectionChanged(e) { var target = e.target; loadEntries(target.value); clearTextarea(); curIndex = -1; showNext(); log('总共'+entries.length+'个, 当前第'+(curIndex+1)+'个'); } function loadEntries(days) { var now = new Date().getTime(); var arr = []; for(var i=0; i<localStorage.length; i++) { var time = localStorage.key(i); var daysBetween = (now - time)/24/60/60/1000; // console.log(daysBetween); if (daysBetween <= days) { var value = JSON.parse(localStorage.getItem(time)); var entry = {time: time, data: value}; arr.push(entry); } } entries = arr; } function saveEntry() { var data_en = $('en').value; var data_zh = $('zh').value; var data = {en: data_en, zh: data_zh}; var time = new Date().getTime(); if (data_en =='' && data_zh == '') // TODO: 正则 { alert('必须输入中英文表达!'); } else { localStorage.setItem(time, JSON.stringify(data)); // Update UI log('保存成功!'); // ReLoad entries loadEntries($('dayselection').value); // Recovery current entry curIndex --; showNext(); } } function deleteEntry() { var currentEntry = entries[curIndex]; if (currentEntry) { localStorage.removeItem(currentEntry.time); // Update UI log('删除成功!'); // Reload entries loadEntries($('dayselection').value); // Go to next entry curIndex --; showNext(); } } function clearStorage() { localStorage.clear(); log('数据初始化完成!'); } function showNext() { if (curIndex +1 <= entries.length-1) { curIndex ++; var entry = entries[curIndex]; showTextarea(entry.data); log('总共'+entries.length+'个, 当前第'+(curIndex+1)+'个'); } } function showPrevious() { if (curIndex-1 >= 0) { curIndex --; var entry = entries[curIndex]; showTextarea(entry.data); log('总共'+entries.length+'个, 当前第'+(curIndex+1)+'个'); } } function showTextarea(data) { var target_en = $('en'); var target_zh = $('zh'); target_en.value = data.en; target_zh.value = data.zh; } function clearTextarea() { $('en').value = ''; $('zh').value = ''; } function $(id) { return document.getElementById(id); } function log(msg) { var target = document.getElementById('msg'); target.innerHTML = msg; } </script> </head> <body> <div style="position: absolute; top: 50px; left: 50px;"> 回顾:<select id="dayselection"> <option value="1"> 1天内 </option> <option value="0.04166666667"> 1小时内 </option> <option value="3"> 3天内 </option> <option value="7"> 7天内 </option> <option value="14"> 14天内 </option> <option value="30"> 1月内 </option> <!-- <option value=""> </option> --> </select> </tr> <input type="button" id="prev" value="上一个" onclick="showPrevious();"> <input type="button" id="next" value="下一个" onclick="showNext();"> <hr> 英语表达:<br/> <textarea id="en" cols="40" rows="6"></textarea> <hr> 中文解释:<br/> <textarea id="zh" cols="40" rows="6"></textarea><br/> <input type="button" id="save" value="保存内容" onclick="saveEntry();"> <input type="button" id="delete" value="删除内容" onclick="deleteEntry();"> <hr> <p id="msg"></p> <div> </body> </html>
登录后复制

【相关推荐】

1. HTML5中Localstorage的使用教程_html5教程技巧

2. 浅谈html5 响应式布局

3. HTML5 程序设计

以上就是分享html5中localStorage的实例教程的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是靓丽洋葱最近收集整理的关于分享html5中localStorage的实例教程的全部内容,更多相关分享html5中localStorage内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部