检查密钥是否已存在后,我尝试在 localStorage中设置,但出现此错误。任何人都知道发生了什么,我该如何解决?
chrome.storage.sync.get($div.attr('id'),function(items){
var lastError = chrome.runtime.lastError;
if (lastError) console.log($div.attr('id')+" does not exist.\n", lastError);
else chrome.storage.sync.set({$div.attr('id'):$div.html()}, function(){}); //I'm having the error from this line
});
未捕获的 SyntaxError:意外的字符串
编辑:
显然这与 attr('id') 有关
,因为我创建了一个变量,然后问题就消失了。还是非常感谢。
这是有效的:
var myobj = {}, key = $div.attr('id');
myobj[key] = $div.html();
chrome.storage.sync.get(key,function(items){
var lastError = chrome.runtime.lastError;
if (lastError) console.log(key+" does not exist.\n", lastError);
else chrome.storage.sync.set(myobj, function(){}); //The error is gone
});