2 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
直接給你段代碼好了,自己看看改改
var strKey = "content_{{$question->id}}";
var storage = window.localStorage;
var docAnswer = document.getElementById("answer_editor_content");
if(docAnswer){
//再次打開頁(yè)面時(shí)嘗試賦值
if(docAnswer.value == "" && storage.getItem(strKey)!=null){
$("#answer_editor_content").val(storage.getItem(strKey));
$("#answer_editor").html(storage.getItem(strKey));
}
//定時(shí)保存
setInterval("cacheContent()",1000);
}
function cacheContent(){
var strValue = document.getElementById("answer_editor_content").value;
if (storage && strValue != '') {
storage.setItem(strKey, strValue);
}
}

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果是寫chrome擴(kuò)展的話,可以使用storage這個(gè)api,會(huì)自動(dòng)同步云端(如果你連接了谷歌服務(wù)器),否則和localstorage是一樣的,可直接存儲(chǔ)數(shù)組或?qū)ο蟆?/p>
具體使用
chrome.storage.sync.set({ 'key': vlaue }, function() {
console.log(' saved success');
});
value可以為字符串,數(shù)組,對(duì)象,使用這個(gè)api需要在manifest.json中添加"storage"這個(gè)權(quán)限
如果是使用普通的sessionStorage或localStorage,存儲(chǔ)復(fù)雜對(duì)象,可以把對(duì)象或數(shù)組用JSON.stringfy轉(zhuǎn)成字符串來(lái)存儲(chǔ),使用的時(shí)候用JSON.parse來(lái)解析成原來(lái)的格式。
希望能對(duì)你有所幫助。
添加回答
舉報(bào)