2 回答

TA贡献1789条经验 获得超8个赞
直接给你段代码好了,自己看看改改
var strKey = "content_{{$question->id}}";
var storage = window.localStorage;
var docAnswer = document.getElementById("answer_editor_content");
if(docAnswer){
//再次打开页面时尝试赋值
if(docAnswer.value == "" && storage.getItem(strKey)!=null){
$("#answer_editor_content").val(storage.getItem(strKey));
$("#answer_editor").html(storage.getItem(strKey));
}
//定时保存
setInterval("cacheContent()",1000);
}
function cacheContent(){
var strValue = document.getElementById("answer_editor_content").value;
if (storage && strValue != '') {
storage.setItem(strKey, strValue);
}
}

TA贡献1836条经验 获得超13个赞
如果是写chrome扩展的话,可以使用storage这个api,会自动同步云端(如果你连接了谷歌服务器),否则和localstorage是一样的,可直接存储数组或对象。
具体使用
chrome.storage.sync.set({ 'key': vlaue }, function() {
console.log(' saved success');
});
value可以为字符串,数组,对象,使用这个api需要在manifest.json中添加"storage"这个权限
如果是使用普通的sessionStorage或localStorage,存储复杂对象,可以把对象或数组用JSON.stringfy转成字符串来存储,使用的时候用JSON.parse来解析成原来的格式。
希望能对你有所帮助。
添加回答
举报