通过简单的案例使用,让大家可以快速上手使用localforage
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>离线存储localforage</title>
<script src="https://cdn.bootcss.com/localforage/1.7.3/localforage.min.js"></script>
</head>
<body>
<br>
<span id="s1" title="第1个存储的数据"></span>
<input type="text" id="t1" placeholder="请输入第1个要存储的数据">
<button type="button" onclick="t1()">点击存储</button>
<br><br><br>
<span id="s2" title="第2个存储的数据"></span>
<input type="text" id="t2" placeholder="请输入第2个要存储的数据">
<button type="button" onclick="t2()">点击存储</button>
<br>
</body>
<script type="text/javascript">
function t1(){
let text = document.getElementById("t1").value;
localforage.setItem('t1', text).then(function (value) {
// 当值被存储后,可执行其他操作
console.log(value);
}).catch(function(err) {
// 当出错时,此处代码运行
console.log(err);
});
}
function t2(){
let text = document.getElementById("t2").value;
localforage.setItem('t2', text).then(function (value) {
// 当值被存储后,可执行其他操作
console.log(value);
}).catch(function(err) {
// 当出错时,此处代码运行
console.log(err);
});
}
</script>
</html>
原文:https://www.cnblogs.com/caominjie/p/11774607.html