我不知道我在json.parse上面的语法错误是什么问题我使用下面的代码
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
function main() {
var data = {
"a":"something1",
"b":"something2"
};
sessionStorage.setObject('data',data);
var newData = sessionStorage.getObject('data');
console.log(newData);
}
Run Code Online (Sandbox Code Playgroud)
在调用getObject('data')时,我在"firefox"中得到了错误,而chrome中的"no error"帮助我找出问题,我分别在示例代码上面运行,它对我来说很好,但是在我的项目中我正在做同样它会导致错误.
我尝试使用mustache.js进行模板化,但我的基本代码不起作用请帮助我出错的地方 -
var person = {
firstName: "Christophe",
lastName: "Coenraets"
};
var template = "<h1>{{firstName}} {{lastName}}</h1>";
var output = Mustache.render(template, person);
document.getElementById('result1').innerHTML = output;
Run Code Online (Sandbox Code Playgroud)
上面的代码正在运行,但下面的代码不起作用: -
这一行写在我的.html页面中:
<script id="sample_template" type="text/template">
<h1>{{firstName}} {{lastName}}</h1>
</script>
Run Code Online (Sandbox Code Playgroud)
这一行写在我的.js文件中:
var data ={
firstName: "Christophe",
lastName: "Coenraets"
};
var template = $('#sample_template').html();
//console.log(template); this prints the html content in console
var info = Mustache.to_html(template, data);
$('#result1').html(info);
Run Code Online (Sandbox Code Playgroud)